andrewmanuja
Well-known member
- Joined
- May 30, 2019
- Messages
- 75
- Programming Experience
- Beginner
Hi All,
I am trying to implement the update data record(s) from a data grid view to the database table.
I got three Layers implemented and need to update changes to the data grid view data using a data table into my database table.
In my Data Access Layer, the respective code looks like below;
My Business layer logic looks like below;
And finally, my presentation layer respective data update button event looks like below;
I am successfully loading the data into the dataGridView in the form load event.
However, when running the program, I am getting the error, "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Iconvertible'.
Appreciate your feedback to correct the issue please.
Kind regards,
Andrew
I am trying to implement the update data record(s) from a data grid view to the database table.
I got three Layers implemented and need to update changes to the data grid view data using a data table into my database table.
In my Data Access Layer, the respective code looks like below;
C#:
public int Update(string textFirstName, string textLastName, int textAge)
{
var returnValue = 0;
using (SqlConnection conn = new SqlConnection(_connStr))
{
using (var dCmd = new SqlCommand(@"UPDATE tbl_Persons SET LastName = @aLastName, Age = @aAge
WHERE FirstName = @aFirstName", conn))
using (var adapter = new SqlDataAdapter { UpdateCommand = dCmd })
{
conn.Open();
SqlParameter[] prms = new SqlParameter[3];
prms[0] = new SqlParameter("@FirstName", SqlDbType.VarChar, 50);
prms[0].Value = textFirstName;
prms[1] = new SqlParameter("@LastName", SqlDbType.VarChar, 50);
prms[1].Value = textLastName;
prms[2] = new SqlParameter("@Age", SqlDbType.Int);
prms[2].Value = textAge;
dCmd.Parameters.AddRange(prms);
returnValue = dCmd.ExecuteNonQuery();
dCmd.Parameters.Clear();
adapter.Update(table);
}
foreach (DataRow row in table.Rows)
{
if (row["Select Item"] as bool? == true)
{
table.AcceptChanges();
row.SetAdded();
}
}
}
return returnValue;
}
My Business layer logic looks like below;
C#:
public int Update(string textFirstName, string textLastName, int textAge)
{
return new PersonDAL().Update(textFirstName, textLastName, textAge);
}
And finally, my presentation layer respective data update button event looks like below;
C#:
var result = 0;
try
{
result = new PersonBAL().Update(textFirstName.ToString(), textLastName.ToString(), Convert.ToInt32(textAge));
if (result > 0)
{
MessageBox.Show("Record updated successfully !");
}
else
{
MessageBox.Show("No record Identified.");
}
}
catch (Exception ee)
{
MessageBox.Show("Error occured. " + ee.Message);
}
I am successfully loading the data into the dataGridView in the form load event.
However, when running the program, I am getting the error, "Unable to cast object of type 'System.Windows.Forms.DataGridViewTextBoxColumn' to type 'System.Iconvertible'.
Appreciate your feedback to correct the issue please.
Kind regards,
Andrew