I have a datagridview with the following columns in c#.net
1.checkboxcolumn
2.Issue Id
3.Book No
4.Book Name
5.Issue Date
6.Due Date
7.Remarks
8.Return Date
i have a textbox field named Member no
i have written code in text_leave event to fetch issue id, book no,book name,issuedate,due date
and remarks from return table and fill the datagridview for the member no in textbox.
now what i need is to update the return table date field for rows where the checkbox field is checked.
For me only one row is updating.what is wrong
My code is:
DateTime returndt;
int issueid;
bool chk;
for (int i = 0; i < dataGridViewEx1.Rows.Count-1; i++)
{
DataGridViewRow dr = dataGridViewEx1.Rows[i];
returndt= DateTime.Parse(dr.Cells[7].Value.ToString());
issueid=int.Parse(dr.Cells[1].Value.ToString());
chk= ((bool)dr.Cells[0].Value);
if (chk == true)
{
try
{
con = db.open();
getdata = new SqlCommand("Update bookissue_return set returned_date='" + returndt.ToString("MM/dd/yyyy") + "',return_status='R' where issue_id=" + issueid + "", con);
getdata.ExecuteNonQuery();
db.close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
MessageBox.Show("Records Updated Successfully", "easyLib", MessageBoxButtons.OK);
this.Dispose();
this.Close();
}
}
pls help and thanks in advance
Loading
VulpesPosted Aug 15, 2012, 4:54 AM
MessageBox.Show("Records Updated Successfully", "easyLib", MessageBoxButtons.OK);
this.Dispose();
this.Close();
is within the if(chk == true) clause and so, when the first row with a checked checkbox is encountered, that row is updated in the database and the form then closes.
You therefore need to remove it outside the 'for' loop:
DateTime returndt;
int issueid;
bool chk;
for (int i = 0; i < dataGridViewEx1.Rows.Count-1; i++)
{
DataGridViewRow dr = dataGridViewEx1.Rows[i];
returndt= DateTime.Parse(dr.Cells[7].Value.ToString());
issueid=int.Parse(dr.Cells[1].Value.ToString());
chk= ((bool)dr.Cells[0].Value);
if (chk == true)
{
try
{
con = db.open();
getdata = new SqlCommand("Update bookissue_return set returned_date='" + returndt.ToString("MM/dd/yyyy") + "',return_status='R' where issue_id=" + issueid + "", con);
getdata.ExecuteNonQuery();
db.close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
MessageBox.Show("Records Updated Successfully", "easyLib", MessageBoxButtons.OK);
this.Dispose();
this.Close();
ullas KPosted Aug 15, 2012, 5:15 AM
ullas KPosted Aug 15, 2012, 4:39 AM
Krishna GaradPosted Aug 14, 2012, 9:40 AM