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();
}
}
7 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vinay SinghPosted Aug 1, 2016, 1:30 PM
Atul RokadePosted Aug 1, 2016, 1:00 PM
Atul RokadePosted Jul 30, 2016, 1:58 PM
Manas MohapatraPosted Jul 29, 2016, 12:50 PM
Atul RokadePosted Jul 29, 2016, 6:02 AM
bool accountHasValue = int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["Availability"].Value.ToString(), out Availability);
if (accountHasValue)
{
cmd.Parameters.AddWithValue("@Availability", Availability);
}
else
{
cmd.Parameters.AddWithValue("@Availability", DBNull.Value);
}
Atul RokadePosted Jul 29, 2016, 5:43 AM
Manas MohapatraPosted Jul 29, 2016, 12:00 AM