数值在accessdatabase中插入为0

时间:2016-06-24 18:44:14

标签: ms-access c#-3.0

我正在创建一个应用程序,其中使用DataGridView将数据插入到访问表中。问题是正确插入了两个值,但是当我尝试插入数值时,它插入为0.

private void dataGridView1_RowLeave(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        if (dataGridView1.IsCurrentRowDirty)
        {
            string 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;
            bool total_availableHasValue = int.TryParse(dataGridView1.Rows[e.RowIndex].Cells["total_available"].Value.ToString(), out Availability);
            cmd.Parameters.AddWithValue("@total_available", Availability);

            con.Open();
            int n = cmd.ExecuteNonQuery();
            con.Close();
            if (n > 0)
            {
                MessageBox.Show("Data Inserted Successfully", "Data Inserted ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }  
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

据推测"可用性"是数值。您确定total_available的datagridview中的值是否为数字?如果不是,则可用= =。

您可以在解析或log / debug之后尝试测试total_availableHasValue来检查datagridview单元格的内容。