Click here to Skip to main content
15,907,183 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have used this code to insert value in the database , but it is not working....
can any one help me!!!!!


C#
private void savbtn_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.schoolConnectionString"].ConnectionString);
                con.Open();
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "insert into Registration values('" + textBox1.Text + "','" + maskedTextBox1.Text + "','" + maskedTextBox2.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + maskedTextBox3.Text + "','" + textBox8.Text + "','" + textBox9.Text + "'," + textBox10.Text + ")";
                com.ExecuteNonQuery();
                MessageBox.Show("Record is saved");
                autoNo();
                maskedTextBox2.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
                textBox6.Clear();
                textBox7.Clear();
                maskedTextBox3.Clear();
                textBox8.Clear();
                textBox10.Clear();
            }
            catch
            {
                MessageBox.Show("error:");
            }
        }
Posted
Comments
AmitGajjar 6-Jun-12 6:41am    
what error you are getting ? have you debug your code ?
Sandeep Mewara 6-Jun-12 6:46am    
Elaborate "not working". Error?

Hi first of all , it's not safety to insert a value to SQL via text.
It would be better to use SqlParameter class instead, to put values to DB.

C#
try
            {
                SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WindowsFormsApplication1.Properties.Settings.schoolConnectionString"].ConnectionString);
                con.Open();
                SqlCommand com = new SqlCommand();
                com.Connection = con;
                com.CommandText = "insert into Registration(Name,Id) values(@p1,@p2 ...);
                
var p1=new SqlParameter("@p1", SqlDbType.VarChar);
p1.Value="Hello";
com.Parameters.Add(p1);

var p2=new SqlParameter("@p2", SqlDbType.Int);
p2.Value=78;
com.Parameters.Add(p2);

com.ExecuteNonQuery();
                MessageBox.Show("Record is saved");
                autoNo();
                maskedTextBox2.Clear();
                textBox2.Clear();
                textBox3.Clear();
                textBox4.Clear();
                textBox5.Clear();
                textBox6.Clear();
                textBox7.Clear();
                maskedTextBox3.Clear();
                textBox8.Clear();
                textBox10.Clear();
            }
            catch
            {
                MessageBox.Show("error:");
            }
 
Share this answer
 
v3
Comments
Maciej Los 6-Jun-12 18:56pm    
Good answer, Oleksandr, my 5!
But take a look at the second parameter. Its type is INT, but you're trying to add VARCHAR. Please, correct it.
Oleksandr Kulchytskyi 7-Jun-12 1:44am    
Yep sorry)) it's my fault. I wrote this code on the fly)
Second thought, read about ADO.NET here and it will help you.

Look at these:
MSDN: ADO.NET[^]
MSDN: Accessing Data with ADO.NET[^]


Further, look here for parameterized query and it's usage to avoid SQL Injection (which is highly possible the way you have coded right now!):
MSDN: Configuring Parameters and Parameter Data Types (ADO.NET)[^]
MSDN: DataAdapter Parameters (ADO.NET)[^]
MSDN: SqlCommand.Parameters Property [^]
 
Share this answer
 
Comments
Maciej Los 6-Jun-12 18:57pm    
Good answer with many useful links, my 5!
you r not mentioned ' ' in " + textBox10.Text + " please keep inside ' ' like ' " +textBox10.Text+"'

i think its work
 
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