button error in my form? [Resolved]

Posted by Sudheep.grandhe under C# on 11/16/2011 | Points: 10 | Views : 1157 | Status : [Member] | Replies : 8
Hi,

insert or update is going with null value .
In my update button click, if txt_VAT is null i have to give a message and set the focus to txt_VAT text box.
private void btn_update_Click(object sender, EventArgs e)
{
MessageBox.Show("please enter the vat:");
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_vat set vat=@vat where vatid=1", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
if (cmd.ExecuteNonQuery() != 1)
{
cmd = new SqlCommand("insert into tbl_vat (VatId,Vat) values('1',@vat)", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
cmd.ExecuteNonQuery();
}
con.Close();
txt_VAT.Text = "";
MessageBox.Show("vat updated succesfully");
//DataTable dt = new DataTable();
//da.Fill(dt);

}

Best,
Sudheep.



Responses

Posted by: Blessyjees on: 11/17/2011 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

Try like this

if (txt_VAT.Text == string.Empty) 

{
MessageBox.Show("please enter vat:");
txt_VAT.Focus();
}
else
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_vat set vat=@vat where vatid=1", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
if (cmd.ExecuteNonQuery() != 1)
{
cmd = new SqlCommand("insert into tbl_vat (VatId,Vat) values('1',@vat)", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
cmd.ExecuteNonQuery();
}
con.Close();
txt_VAT.Text = "";
MessageBox.Show("vat updated succesfully");
//DataTable dt = new DataTable();
//da.Fill(dt);

}


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: Walia_jagwinder on: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
what error you are getting?

One person's data is another person's program.

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

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

Up
0
Down
Hi,

without entering any details in the textbox and click on the button it shows the message as "added successfully".if the user click on the button it has to show the "please enter the details like that"

Best,
Sudheep.

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

Posted by: perfectchourasia-9163 on: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
For this please use dotnet validation such as required fiel validator etc.

<html>
<body>

<form runat="server">
Name: <asp:TextBox id="name" runat="server" />
<br />
Age: <asp:TextBox id="age" runat="server" />
<br /><br />
<asp:Button runat="server" Text="Submit" />
<br /><br />
<asp:RequiredFieldValidator
ControlToValidate="name"
Text="The name field is required!"
runat="server" />
</form>

</body>
</html>


for validation study please check on google or go to

http://www.beansoftware.com/ASP.NET-Tutorials/Validation-In-ASP.NET.aspx

ER sandeep chourasia
sandeepchrs@yahoo.com (on facebook)
http://www.aspnetcodes.com/

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

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

Up
0
Down
Hi,

what i posted in the forum and what you are giving the reply to me.i am developimg windows applications.
this is my question:
insert or update is going with null value
In my update button click, if txt_VAT is null i have to give a message and set the focus to txt_VAT text box.

Best,
Sudheep.

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

Posted by: Blessyjees on: 11/17/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi

Try this at the starting of the update button click.
 if (txt_VAT.Text== string.Empty)

{
MessageBox.Show("hi");
txt_VAT.Text.Focus();
}


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: 11/17/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,
it shows the same error.if txt_vat is null and click the update button it has to shows the message as please enter something.if i tried below code it wont execute.


private void btn_update_Click(object sender, EventArgs e)
{
if (txt_VAT.Text == string.Empty)
{
MessageBox.Show("please enter vat:");
txt_VAT.Focus();
}
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_vat set vat=@vat where vatid=1", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
if (cmd.ExecuteNonQuery() != 1)
{
cmd = new SqlCommand("insert into tbl_vat (VatId,Vat) values('1',@vat)", con);
cmd.Parameters.AddWithValue("@vat", txt_VAT.Text);
cmd.ExecuteNonQuery();
}
con.Close();
txt_VAT.Text = "";
MessageBox.Show("vat updated succesfully");
//DataTable dt = new DataTable();
//da.Fill(dt);

}



Best,
Sudheep.

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

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

Up
0
Down
Hi,

thankyou mam your help me a lot for all the issues .keep doing mam.

Best,
Sudheep.

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

Login to post response