Hi
below code is an update code. In this message boxes are not working properly. When I click the update button it always display the "record has been updated" message box.
Please help me..
private void ButtUpdate_Click(object sender, EventArgs e)
{
int x;
da.UpdateCommand = new SqlCommand("UPDATE EWO_DETAILS SET CONTRACTOR_CODE = @ CONTRACTOR_CODE, START_DATE = @START_DATE, END_DATE = @END_DATE, CREATED_BY = @CREATED_BY , CREATED_DATE = @CREATED_DATE WHERE EWO_NO = @EWO_NO", cs);
da.UpdateCommand.Parameters.Add("@CONTRACTOR_CODE", SqlDbType.VarChar).Value = TxtContractorCode.Text;
da.UpdateCommand.Parameters.Add("@EWO_SPEC", SqlDbType.VarChar).Value = TxtEwoSpec.Text;
da.UpdateCommand.Parameters.Add("@CAT", SqlDbType.VarChar).Value = TxtJcat.Text;
da.UpdateCommand.Parameters.Add("@MAIN", SqlDbType.VarChar).Value = TxtJmain.Text;
da.UpdateCommand.Parameters.Add("@SPEC", SqlDbType.VarChar).Value = RTBSpec.Text;
da.UpdateCommand.Parameters.Add("@SUB", SqlDbType.VarChar).Value = TxtSub.Text;
da.UpdateCommand.Parameters.Add("@XTC", SqlDbType.VarChar).Value = TxtExtc.Text;
da.UpdateCommand.Parameters.Add("@START_DATE", SqlDbType.VarChar).Value = StartDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@END_DATE", SqlDbType.VarChar).Value = EndDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@CREATED_BY", SqlDbType.VarChar).Value = TxtCreatedBy.Text;
da.UpdateCommand.Parameters.Add("@CREATED_DATE", SqlDbType.VarChar).Value = CreatedDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@EWO_NO", SqlDbType.Int).Value = ds.Tables[0].Rows[bs.Position][0];
cs.Open();
try
{
x = da.UpdateCommand.ExecuteNonQuery();
cs.Close();
if (x >= 1)
{
MessageBox.Show("Record has been Updated ");
}
else
{
MessageBox.Show("Record Already Updated ", "Error");
}
}
catch
{
MessageBox.Show("Error on update please reloggin", "Information");
cs.Close();
}
}
Sanjeeb LenkaPosted Sep 10, 2013, 1:08 PM
two records are present in you table with id 1 and 2
you are trying to update the 2nd record with id 1 but the id is already present 1st record. so you have to check that on update not to allow it
neesha herathPosted Sep 10, 2013, 12:31 PM
I can not understand what you are saying.could you plz explain it again?
Sanjeeb LenkaPosted Sep 10, 2013, 12:13 PM
Another thing you have to add that you have to check if one id was present in table you cant update that in other record id like insert of record.
neesha herathPosted Sep 10, 2013, 10:56 AM
Thankz a lot. . .
I edit my code as you mentioned. now it's ok. below is the running code
private void ButtUpdate_Click(object sender, EventArgs e)
{
try
{
if (TxtWorkOrderNo.Text == "")
{
MessageBox.Show("Please enter a Work Order Number");
}
else if (TxtJcat.Text == "")
{
MessageBox.Show("Please enter JCAT");
}
else if (TxtJmain.Text == "")
{
MessageBox.Show("Please enter JMAIN");
}
else if (TxtSub.Text == "")
{
MessageBox.Show("Please enter SUB");
}
else if (RTBSpec.Text == "")
{
MessageBox.Show("Please enter SPEC");
}
else if (TxtEwoSpec.Text == "")
{
MessageBox.Show("Please enter EWO_SPEC");
}
else if (TxtExtc.Text == "")
{
MessageBox.Show("Please enter EXTC");
}
else if (TxtContractorCode.Text == "")
{
MessageBox.Show("Please enter Contractor Code");
}
else if (TxtCreatedBy.Text == "")
{
MessageBox.Show("Please enter Created By ?");
}
else
{
int x;
da.UpdateCommand = new SqlCommand("UPDATE EWO_DETAILS SET CONTRACTOR_CODE = @ CONTRACTOR_CODE, START_DATE = @START_DATE, END_DATE = @END_DATE, CREATED_BY = @CREATED_BY , CREATED_DATE = @CREATED_DATE WHERE EWO_NO = @EWO_NO", cs);
da.UpdateCommand.Parameters.Add("@CONTRACTOR_CODE",SqlDbType.VarChar).Value = TxtContractorCode.Text;
da.UpdateCommand.Parameters.Add("@EWO_SPEC", SqlDbType.VarChar).Value = TxtEwoSpec.Text;
da.UpdateCommand.Parameters.Add("@CAT", SqlDbType.VarChar).Value = TxtJcat.Text;
da.UpdateCommand.Parameters.Add("@MAIN", SqlDbType.VarChar).Value = TxtJmain.Text;
da.UpdateCommand.Parameters.Add("@SPEC", SqlDbType.VarChar).Value = RTBSpec.Text;
da.UpdateCommand.Parameters.Add("@SUB", SqlDbType.VarChar).Value = TxtSub.Text;
da.UpdateCommand.Parameters.Add("@XTC", SqlDbType.VarChar).Value = TxtExtc.Text;
da.UpdateCommand.Parameters.Add("@START_DATE", SqlDbType.VarChar).Value = StartDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@END_DATE", SqlDbType.VarChar).Value = EndDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@CREATED_BY", SqlDbType.VarChar).Value = TxtCreatedBy.Text;
da.UpdateCommand.Parameters.Add("@CREATED_DATE",SqlDbType.VarChar).Value = CreatedDatePicker.Value.Date;
da.UpdateCommand.Parameters.Add("@EWO_NO", SqlDbType.Int).Value = ds.Tables[0].Rows[bs.Position][0];
cs.Open();
x = da.UpdateCommand.ExecuteNonQuery();
cs.Close();
if (x >= 1)
{
MessageBox.Show("Record has been Updated ");
}
}
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
cs.Close();
}
}
Suthish NairPosted Sep 10, 2013, 4:24 AM
Sanjeeb LenkaPosted Sep 10, 2013, 1:05 AM
update means it will update the values when you click. so its correct only. another thing is that you have to again do the validation of text box in update also.