Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why this code is inserting one empty record on the top of the table..?

C#
protected void Button1_Click(object sender, EventArgs e)
   {
       try
       {
           string Register = "INSERT INTO Userregistrationtable(Emailid,Dname,Mobileno,passwd,Gender,City,Area,Postal,Adress,DOB,Uskill,dream,Bestfrnd,Favplace,Favdish,Hobbies,InterestIn) VALUES('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + RadioButtonList1.SelectedValue + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox21.Text + "','" + TextBox20.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox17.Text + "','" + TextBox18.Text + "','" + TextBox19.Text + "','" + DropDownList4.SelectedValue + "')";
           dbClass.ConnectDataBaseToInsert(Register);

       }
       catch (Exception a)
       {

       }
       TextBox1.Text = "";
       TextBox2.Text = "";
       TextBox3.Text = "";
       TextBox4.Text = "";
       TextBox5.Text = "";
       TextBox6.Text = "";
       TextBox7.Text = "";
       TextBox8.Text = "";
       TextBox21.Text = "";
       TextBox20.Text = "";
       TextBox9.Text =  "";
       TextBox10.Text = "";
       TextBox11.Text = "";
       TextBox17.Text = "";
       TextBox18.Text = "";
       TextBox19.Text = "";
       DropDownList4.SelectedIndex=0;

   }
Posted
Updated 13-Dec-11 23:16pm
v2
Comments
Shobana16 14-Dec-11 5:46am    
Can you say what is the error did you get?

1 solution

There are so many things wrong with that, that I'm not surprised that it doesn't do what you want.
1) Don't use VS Default names - You may remember today that TextBox11 holds the Favourite place (or is that TextBox17?) but you won't next week. Use sensible names instead.
2) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
3) Don't trap an error and then ignore it - or strange things happen and you don't know why. Errors mean there is a problem: fix it, or report it. Never ignore it.
4) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
5) Never assume that the user can type: check the email address could be valid, the mobile number is a number rather than a rude message. And so on.

Do the above, and then try again.
 
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