13
Answers

Numeric value insert as 0 in accessdatabase

Atul Rokade

Atul Rokade

8y
404
1
Iam creating one application where data inserted into access table using datagridview but issue is two value(s) are inserted correctly but when im trying to insert numeric value its inserted as 0
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.IsCurrentRowDirty)
{
string connectionString = null;
connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
con.ConnectionString = connectionString;
string cmd1 = "insert into Medicine_Available_Detail([Medicine_Name],[Dealer_name],[total_available])values(?,?,?)";
OleDbCommand cmd = new OleDbCommand(cmd1, con);
cmd.CommandType = CommandType.Text;
string Medicine_Name = dataGridView1.Rows[e.RowIndex].Cells["Medicine_Name"].Value.ToString();
cmd.Parameters.AddWithValue("@Medicine_Name", Medicine_Name);
string Dealer_name = dataGridView1.Rows[e.RowIndex].Cells["Dealer_name"].Value.ToString();
cmd.Parameters.AddWithValue("@Dealer_name", Dealer_name);
 
 
int Availability=0;
bool total_availablehasvalue = int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["total_available"].Value.ToString(), out Availability);
if (total_availablehasvalue)
{
cmd.Parameters.AddWithValue("@total_available", Availability);
}
else
{
cmd.Parameters.AddWithValue("@total_available", DBNull.Value);
}
 
}

Answers (13)