Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
private void button2_Click(object sender, EventArgs e)
       {
           string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ravi\Documents\OnlineBanking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
           SqlConnection conn = new SqlConnection(con);
           conn.Open();
           SqlCommand chk = new SqlCommand("Insert into Registration(Name)values(@Name)",conn);

           chk.Parameters.AddWithValue("@Name", textBox1.Text);
           chk.ExecuteNonQuery();

           MessageBox.Show("Account created successfully!!!");
       }


What I have tried:

I am unable to insert value into database.. plz rectify my error.
Posted
Updated 2-Apr-16 7:45am
Comments
Karthik_Mahalingam 2-Apr-16 12:59pm    
what is the error message ?

1 solution

Hello, Try this code

C#
private void button2_Click(object sender, EventArgs e)
       {
           string con = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Ravi\Documents\OnlineBanking.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
           SqlConnection conn = new SqlConnection(con);
           SqlCommand chk = new SqlCommand("Insert into Registration(Name)values(@Name)",conn);
	chk .CommandType = CommandType.Text;
    chk .CommandText = sqlQuery;
    chk .Parameters.Add("@Name", textBox1.Text);
     chk .Open();
    chk .ExecuteNonQuery();
    chk .Close();
 
           MessageBox.Show("Account created successfully!!!");
       }
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 2-Apr-16 13:51pm    
what is that sqlQuery in the below line
chk .CommandText = sqlQuery;

the commandText is already defined in
SqlCommand chk = new SqlCommand("Insert into Registration(Name)values(@Name)",conn);

Tips: whenever posting a solution, try to add the codes in the Code block.
Thanks
KARTHIK
Arvind Zamakia 2-Apr-16 14:04pm    
comment this line and try now
Karthik_Mahalingam 2-Apr-16 14:57pm    
ha ha,why should i try.. let the OP try.
HellHotie 2-Apr-16 22:28pm    
Sorry it was my mistake have been resolved.. ty

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