Atul Rokade

Atul Rokade

  • NA
  • 141
  • 44.1k

How to insert integer value in access database?

Jul 28 2016 5:39 AM
I am trying to insert Integer value into MS-Access database by using datagridview, but other string values are entered successfully but integer is not. I use debugger also but when debugger come to that line its showing me null value. In my database I use Memo as datatype for Availability, here is my code and possible snapshot
private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
try
{
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,Availability) values(?,?,@Availability)";
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);
string Availability = dataGridView1.Rows[e.RowIndex].Cells["Availability"].Value.ToString();
cmd.Parameters.AddWithValue("@Availability", Availability);
con.Open();
int n = cmd.ExecuteNonQuery();
con.Close();
if (n > 0)
{
MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
catch (Exception ex)
{
Load_data();
dataGridView1.Refresh();
}
}

Answers (7)