Click here to Skip to main content
15,897,718 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why records is not going to table ?

My cs code is given below

private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=amsco;User ID = sa;Password=123";
            cnn = new SqlConnection(connetionString);
            SqlDataAdapter da = new SqlDataAdapter();
            try
            {
                da.InsertCommand = new SqlCommand("INSERT INTO employee VALUES(@eid,@ename)", cnn);
                da.InsertCommand.Parameters.Add("@eid", SqlDbType.VarChar).Value = textBox1.Text;
                da.InsertCommand.Parameters.Add("@ename", SqlDbType.VarChar).Value = textBox2.Text;
                cnn.Open();
                da.InsertCommand.ExecuteNonQuery();
                cnn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Can not open connection ! ");
                MessageBox.Show(ex.Message);
            }
        }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 19-Mar-12 23:22pm
v2
Comments
uspatel 20-Mar-12 5:25am    
what error occured?

That's difficult to say - how do you know they aren't?
First off, run a copy of SQL Server Management Studio, and run a query listing all the rows in the employee table.
Then put a breakpoint on the line:
C#
connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=amsco;User ID = sa;Password=123";
Run you app in the debugger and click your button. When the debugger stops execution, single step through the code.
Do you get through each line and to the end of the method?
If not, then you should have a better idea where the problem is.
If you do, then run a second query in SSMS to list all rows, and compare the two lists. Is the new row in there?
 
Share this answer
 
C#
private void button1_Click(object sender, EventArgs e)
        {
            string connetionString = null;
            SqlConnection cnn;
            connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=amsco;User ID = sa;Password=123";
            cnn = new SqlConnection(connetionString);
            SqlDataAdapter da = new SqlDataAdapter();
                cnn.Open();
            try
            {
                da.InsertCommand = new SqlCommand("INSERT INTO employee VALUES(@eid,@ename)", cnn);
                da.InsertCommand.Parameters.Add("@eid", SqlDbType.VarChar).Value = textBox1.Text;
                da.InsertCommand.Parameters.Add("@ename", SqlDbType.VarChar).Value = textBox2.Text;

                da.InsertCommand.ExecuteNonQuery();
                cnn.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show("Can not open connection ! ");
                MessageBox.Show(ex.Message);
            }
        }
 
Share this answer
 
v2
Comments
qwerty 2 20-Mar-12 6:09am    
hey abhijeet!!

When we are using data adapter..Is it required to give open and close methods for con obj i.e there is no need since adapter opens and closes connection automatically..I would rather prefer checking it through debugger and also querying the database..!!!

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