5
Answers

Insert values into database from ASP.NET page

Ankit Rathore

Ankit Rathore

14y
3.2k
1
Hi Guys,

Here I am facing a problem to insert values into database.
In my database already 2 rows is there, but now I wanted to put from asp.net page.
but at last when I give the value it will take automatically first record only,
and give error like "Primary Key can not be duplicate........"
   
In my code I am doing like that.............


Please help me what can I do ..............?


My  Code is .......

try
        {
            if (Page.IsPostBack)
            {
               
                string InsertString = "Insert into Emp" + "(ID, Name, Designation, TempAddress, PermanentAddress, Salary, Contact)" +
                                       "values('" + txtEmpId.Text + "','" + txtName.Text + "','" + txtDesignation.Text + "','" + txtPAddress.Text + "','" + txtTaddress.Text + "','" + txtSalary.Text + "','" + txtContact.Text + "')";

               


                if (txtEmpId.Text != "" && txtName.Text != "" && txtDesignation.Text != "" && txtPAddress.Text != "" && txtTaddress.Text != "" && txtSalary.Text != "" && txtContact.Text != "")
                {

                    SqlCommand command = new SqlCommand(InsertString, objcConnect.con);
                    objcConnect.dAdapEmp.InsertCommand = command;
                    objcConnect.dAdapEmp.InsertCommand.ExecuteNonQuery();
                    //command.ExecuteNonQuery();

                    MessageBox.Show("Value Inserted");

                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
 
Answers (5)