Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created an Registration page and the data inserted in the page will be stored in the sql server db.But if I use validation and any particular field is not entered than also it stores the remaining data in db how to restrict that????


C#
protected void Button2_Click(object sender, EventArgs e)
{
    // string query;
    string str = "Data Source=HIMANSHU-PC;Initial Catalog=new1;Integrated  Security=True";
    SqlConnection con = new SqlConnection(str);
    con.Open();
    string query = "insert into logindata values('" + txtname.Text + "','" +    txtlname.Text + "','" + txtuname.Text + "','" + txtpass.Text + "','" + txtcity.Text + "','" + dropdwcou.Text + "')";
    SqlCommand cmd = new SqlCommand(query,con);
    cmd.ExecuteNonQuery();
    con.Close();
}
Posted
Updated 15-Oct-14 2:25am
v2
Comments
M.Farrukh Abbas 15-Oct-14 7:24am    
used required field validator and for specific business logic validation use custom validation what is the current issue.
NaibedyaKar 15-Oct-14 7:29am    
What kind of validations you have used? You can also validate the textbox values on serverside code if you want to double sure on the validations.
Member 11132163 15-Oct-14 7:37am    
I have used client side validations
Richard Deeming 15-Oct-14 8:42am    
Your code is susceptible to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

If you are using the client side validatiosn, then things should work fine unless some smart guy is trying to disable javascript and send blank data.
Better to use the requiredfieldvalidator.
http://www.devmanuals.com/tutorials/ms/aspdotnet/requiredfieldvalidator.html[^]
If you want to be more safe then check the textbox null or empty in server side code.
C#
if (!string.IsNullOrEmpty(textbox.text))
{
  //do this
}


I will also like to suggest you to use parameterize query. This will help you from SQL injection. Check^
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900