I have two tables 'Item' and 'Invoice'. And I have a 'datagridview' in my form with headers Now application will fill 'itemcode|item desc|item rate' from 'item' table when user enter 'item desc' cell value ('using auto complete property for item desc cell and key down event for fill according to the user entry'). Now user will enter 'Quantity' and 'Item Total'(by manually for now). And press submit button. Here the problem comes. Its showing me error input string is not correct. But when I enter all cell manually(I mean without using keydown event to fill data) the data is inserting to invoice table.
This is my submit button to insert in invoice table <
private void btn_submit_Click(object sender, EventArgs e)
{
try
{
foreach (DataGridViewRow row in dtgrdvw_items.Rows)
{
using (SqlCommand cmd = new SqlCommand("insert into invoice values (@inv_no,@inv_date,@inv_cust_name,@inv_remarks,@inv_type,@inv_item_code,@inv_item_desc,@inv_item_rate," +
"@inv_item_qty,@inv_item_total)", con))
{
cmd.Parameters.AddWithValue("@inv_no", Convert.ToInt32(txt_inv_no.Text));
cmd.Parameters.AddWithValue("@inv_date", txt_date.Text);
cmd.Parameters.AddWithValue("@inv_cust_name", txt_cust_name.Text);
cmd.Parameters.AddWithValue("@inv_remarks", txt_remarks.Text);
cmd.Parameters.AddWithValue("@inv_type", cmbx_inv_type.SelectedItem);
cmd.Parameters.AddWithValue("@inv_item_code",Convert.ToInt32(row.Cells[0].Value));
cmd.Parameters.AddWithValue("@inv_item_desc",Convert.ToString(row.Cells[1].Value));
cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[2].Value));
cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[3].Value));
decimal total = Convert.ToDecimal(row.Cells[2].Value) * Convert.ToDecimal(row.Cells[3].Value);
cmd.Parameters.AddWithValue("@inv_item_total",total);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
con.Close();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
MessageBox.Show("invoice submitted");
}
>
this is my keydown event
<
private void dtgrdvw_items_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
int rowindex = dtgrdvw_items.CurrentCell.RowIndex;
int columnindex = dtgrdvw_items.CurrentCell.ColumnIndex;
if (columnindex == 1)
{
SqlCommand cmd = new SqlCommand("Select * from item WHERE item_desc=@inv_item_desc", con);
cmd.Parameters.Add("@inv_item_desc", SqlDbType.VarChar).Value = dtgrdvw_items.Rows[rowindex].Cells[columnindex].Value.ToString();
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
dtgrdvw_items.Rows[rowindex].Cells[0].Value = dr[0].ToString();
dtgrdvw_items.Rows[rowindex].Cells[1].Value = dr[1].ToString();
dtgrdvw_items.Rows[rowindex].Cells[3].Value = dr[2].ToString();
}
dr.Close();
}
}
}
>
Please help.
If I understand correctly, the Rate column accepts an integer type. You may want to use Convert.ToInt32
on
dtgrdvw_items.Rows[rowindex].Cells[3].Value = dr[2].ToString();
so that it will cast the string to an integer before it will be assigned to Cells[3] which is the Rate column.
dtgrdvw_items.Rows[rowindex].Cells[3].Value = Convert.ToInt32(dr[2].ToString());
Cells in grid 0 code 1 description 2 quantity 3 rate 4 total
cmd.Parameters.AddWithValue("@inv_item_code",Convert.ToInt32(row.Cells[0].Value));
cmd.Parameters.AddWithValue("@inv_item_desc",Convert.ToString(row.Cells[1].Value));
cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[2].Value)); //Change
Should be:
cmd.Parameters.AddWithValue("@inv_item_rate",Convert.ToDecimal(row.Cells[3].Value));
cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[3].Value)); //Change
Should be:
cmd.Parameters.AddWithValue("@inv_item_qty",Convert.ToInt32(row.Cells[2].Value)); //Change
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.