Click here to Skip to main content
15,905,427 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
namespace Company
{
    public partial class Form2 : Form
    {
        SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\User\Documents\Company.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
        SqlCommand cmd;
        SqlDataAdapter da;
        DataSet ds;
        public Form2()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            con.Open();
            cmd = new SqlCommand("insert into Login(Username,Password,Phone_No) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3 .Text+"'",con);
            cmd. ExecuteNonQuery();
            con.Close();
            MessageBox.Show("User successfully added");
            this.Hide();
            Form1 form1 = new Form1();
            form1.Show();
        }
    }
}
Posted
Updated 24-Apr-14 12:01pm
v2
Comments
bunzitop 25-Apr-14 2:53am    
you are misssing closing bracket in VAULES Clause, near con.

It must be like this

cmd = new SqlCommand("insert into Login(Username,Password,Phone_No) values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3 .Text+"' )",con);
Richard C Bishop 25-Apr-14 13:05pm    
This is not a good suggestion as you are propagating the dangerous practice of string concatenation in queries. This leaves your application open to SQL injection.

1 solution

You should be using parameterized queries to handle this. What you currently have leaves you susceptible to SQL injection.

You want to do something like this:
cmd = new SqlCommand("INSERT INTO Login(Username,Password,Phone_No) VALUES (@UserName,@Password,@Phone_No)",con);
cmd.Parameters.AddWithValue("@UserName",textBox1.Text);
cmd.Parameters.AddWithValue("@Password",textBox2.Text);
cmd.Parameters.AddWithValue("@Phone_No",textBox3.Text);
            cmd. ExecuteNonQuery();
 
Share this answer
 
v2

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