without entering the details it shows message as update successfully? [Resolved]

Posted by Sudheep.grandhe under C# on 12/23/2011 | Points: 10 | Views : 1154 | Status : [Member] | Replies : 6
Hi,

without entering the details it shows message as update successfully?how can i prevent this.please do the needful.here is my code.

private void btn_update_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_report2 set compname=@compname,compaddress=@compaddress,orderno=@orderno,ondate=@ondate,invoicedate=@invoicedate,amountstatus=@amountstatus,netamount=@netamount,tamount=@tamount where invoiceno=@id", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@id", id);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("update tbl_report1 set compname=@compname,orderno=@orderno,invoicedate=@invoicedate,ondate=@ondate where invoiceid=@id", con1);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@id", txt_invoiceno.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
txt_amount.Text = "";
txt_customeraddress.Text = "";
txt_invoicedate.Text = "";
txt_invoiceno.Text = "";
txt_netamount.Text = "";
txt_ondate.Text = "";
txt_orderno.Text = "";
txt_perprice.Text = "";
txt_quantity.Text = "";
txt_totalamount.Text = "";
txt_vat.Text = "";
txt_vatpercentage.Text = "";
MessageBox.Show("updated successfully");
}

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/23/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

You have not called validation function what I've mentioned above.

Try this.
private void btn_update_Click_1(object sender, EventArgs e)

{
if (!Validation())
return;

SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_report2 set compname=@compname,compaddress=@compaddress,orderno=@orderno,ondate=@ondate,invoicedate=@invoicedate,amountstatus=@amountstatus,netamount=@netamount,tamount=@tamount where invoiceno=@id", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@id", id);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("update tbl_report1 set compname=@compname,orderno=@orderno,invoicedate=@invoicedate,ondate=@ondate where invoiceid=@id", con1);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@id", txt_invoiceno.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
txt_amount.Text = "";
txt_customeraddress.Text = "";
txt_invoicedate.Text = "";
txt_invoiceno.Text = "";
txt_netamount.Text = "";
txt_ondate.Text = "";
txt_orderno.Text = "";
txt_perprice.Text = "";
txt_quantity.Text = "";
txt_totalamount.Text = "";
txt_vat.Text = "";
txt_vatpercentage.Text = "";
MessageBox.Show("updated successfully");
}


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Blessyjees on: 12/23/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,

Not clear your request. Not find any issue in the code.

Blessy Baby
Digitalmesh Softech pvt Ltd
https://blessybaby.wordpress.com/

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/23/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Mam,

Thanks for replying to me.without entering details into textboxes if i click on update button it shows updated successfully.how can i prevent this.


Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/23/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try below code.

bool Validation()
{
if (txt_amount.Text.Trim() == "" ||
txt_customeraddress.Text.Trim() == "" ||
txt_invoicedate.Text.Trim() == "" ||
txt_invoiceno.Text.Trim() == "" ||
txt_netamount.Text.Trim() == "" ||
txt_ondate.Text.Trim() == "" ||
txt_orderno.Text.Trim() == "" ||
txt_perprice.Text.Trim() == "" ||
txt_quantity.Text.Trim() == "" ||
txt_totalamount.Text.Trim() == "" ||
txt_vat.Text.Trim() == "" ||
txt_vatpercentage.Text.Trim() == "" )
{


MessageBox.Show("One or many fields are empty.Please provide input for all fields.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
else
return true;
}
private void btn_update_Click_1(object sender, EventArgs e)
{
if (!Validation())
return;
//Write your old code as it is here
}


Change Error message as per your requirement.


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 12/23/2011 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down
Sksingh is right

you must validate the user input and you must not open many Connections and you must use the try...catch to see where is the Problem.

Thank you for posting at Dotnetfunda
[Administrator]

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/23/2011 [Member] Starter | Points: 25

Up
0
Down
private void btn_update_Click_1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_report2 set compname=@compname,compaddress=@compaddress,orderno=@orderno,ondate=@ondate,invoicedate=@invoicedate,amountstatus=@amountstatus,netamount=@netamount,tamount=@tamount where invoiceno=@id", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@id", id);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}
cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("update tbl_report1 set compname=@compname,orderno=@orderno,invoicedate=@invoicedate,ondate=@ondate where invoiceid=@id", con1);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@id", txt_invoiceno.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
txt_amount.Text = "";
txt_customeraddress.Text = "";
txt_invoicedate.Text = "";
txt_invoiceno.Text = "";
txt_netamount.Text = "";
txt_ondate.Text = "";
txt_orderno.Text = "";
txt_perprice.Text = "";
txt_quantity.Text = "";
txt_totalamount.Text = "";
txt_vat.Text = "";
txt_vatpercentage.Text = "";
MessageBox.Show("updated successfully");
}

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response