So what I am doing here is the process of inserting and updating data inside a GridView to the database.
I am facing a little bit of problem when it comes to Updating.
When I insert data into multiple rows of the GridView, it will only update the last row of the GridView.
If the TDR_CODE already existed in the database and lets say it have 3 rows inside the table, then all the 3 rows will have the same value as the last row of the GridView.
Below are the codes related and what it do is the codes have to check the exitance of TDR_CODE inside the table.
If it doesnt exist, then the count will be 0 but if it does, count will be > 0.
Below are the codes
protected void GLType()
{
string QueryCheckExistingTDRCode = "SELECT COUNT(*) FROM BOS_M_TRADER_GL WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand CheckExistingTDRCodeCmd = new OracleCommand(QueryCheckExistingTDRCode, con))
{
CheckExistingTDRCodeCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = TextBoxTDRCode.Text;
int count = Convert.ToInt32(CheckExistingTDRCodeCmd.ExecuteScalar());
if (count == 0)
{
InsertDataGLType();
}
else
{
UpdateDataGLType();
}
}
}
protected void InsertDataGLType()
{
string insertQuery = "INSERT INTO BOS_M_TRADER_GL (TDR_CODE, GL_TYPE, GL_ACCNO) VALUES (:TDR_CODE, :GL_TYPE, :GL_ACCNO)";
try
{
List<UpdateDataItem> updateItems = new List<UpdateDataItem>();
foreach (GridViewRow row in GridViewSelection.Rows)
{
DropDownList ddlGLType = (DropDownList)row.FindControl("DropDownListGLType");
TextBox txtGLAccNo = (TextBox)row.FindControl("TextBoxGLAccNo");
if (!string.IsNullOrEmpty(ddlGLType.SelectedValue) && !string.IsNullOrEmpty(txtGLAccNo.Text))
{
// Add data to the list
updateItems.Add(new UpdateDataItem
{
TDRCode = TextBoxTDRCode.Text,
GLType = ddlGLType.SelectedValue,
GLAccNo = txtGLAccNo.Text
});
}
}
// Now, update the database with the data from the list
using (OracleCommand InsertNewDataCmd = new OracleCommand(insertQuery, con))
{
InsertNewDataCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2);
InsertNewDataCmd.Parameters.Add("GL_TYPE", OracleDbType.Varchar2);
InsertNewDataCmd.Parameters.Add("GL_ACCNO", OracleDbType.Varchar2);
foreach (var item in updateItems)
{
InsertNewDataCmd.Parameters["TDR_CODE"].Value = item.TDRCode;
InsertNewDataCmd.Parameters["GL_TYPE"].Value = item.GLType;
InsertNewDataCmd.Parameters["GL_ACCNO"].Value = item.GLAccNo;
InsertNewDataCmd.ExecuteNonQuery();
}
}
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
Below is the problematic updating codes
protected void UpdateDataGLType()
{
string updateQuery = "UPDATE BOS_M_TRADER_GL SET GL_TYPE = :GL_TYPE, GL_ACCNO = :GL_ACCNO WHERE TDR_CODE = :TDR_CODE";
try
{
List<UpdateDataItem> updateItems = new List<UpdateDataItem>();
foreach (GridViewRow row in GridViewSelection.Rows)
{
DropDownList ddlGLType = (DropDownList)row.FindControl("DropDownListGLType");
TextBox txtGLAccNo = (TextBox)row.FindControl("TextBoxGLAccNo");
if (!string.IsNullOrEmpty(ddlGLType.SelectedValue) && !string.IsNullOrEmpty(txtGLAccNo.Text))
{
// Add data to the list
updateItems.Add(new UpdateDataItem
{
TDRCode = TextBoxTDRCode.Text,
GLType = ddlGLType.SelectedValue,
GLAccNo = txtGLAccNo.Text
});
// You can perform additional actions here if needed
}
}
// Now, update the database with the data from the list
using (OracleCommand UpdateCmd = new OracleCommand(updateQuery, con))
{
UpdateCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2);
UpdateCmd.Parameters.Add("GL_TYPE", OracleDbType.Varchar2);
UpdateCmd.Parameters.Add("GL_ACCNO", OracleDbType.Varchar2);
foreach (var item in updateItems)
{
UpdateCmd.Parameters["TDR_CODE"].Value = item.TDRCode;
UpdateCmd.Parameters["GL_TYPE"].Value = item.GLType;
UpdateCmd.Parameters["GL_ACCNO"].Value = item.GLAccNo;
UpdateCmd.ExecuteNonQuery();
}
}
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
if (string.IsNullOrEmpty(LabelWarning.Text))
{
LabelWarning.Text = "Operation successful!";
}
}
}
Below is the non problematic updating codes
protected void UpdateDataGLType()
{
string deleteQuery = "DELETE FROM BOS_M_TRADER_GL WHERE TDR_CODE = :TDR_CODE";
using (OracleCommand deleteCmd = new OracleCommand(deleteQuery, con))
{
deleteCmd.Parameters.Add("TDR_CODE", OracleDbType.Varchar2).Value = TextBoxTDRCode.Text;
try
{
deleteCmd.ExecuteNonQuery();
}
catch (OracleException oracleEx)
{
LabelWarning.Text = "Oracle Error: " + oracleEx.Message;
}
catch (FormatException formatEx)
{
LabelWarning.Text = "Format Error: " + formatEx.Message;
}
catch (Exception ex)
{
LabelWarning.Text = "Error: " + ex.Message;
}
finally
{
InsertDataGLType();
}
}
}
So to encounter this problem, I delete the existing data from the database initially and then do the insert process.
I just dont understand why I cant just do update eventhough I already put the rows modified inside a list.
I also have tried OracleBulkCopy but the problem still happens.