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());
}
Loading
Dipa AhujaPosted Apr 15, 2010, 8:16 AM
{
try
{
if (Page.IsPostBack)
{
string newid =txtid.Text.ToString();
objcConnect.dAdapEmp.InsertCommand = command;
objcConnect.dAdapEmp.InsertCommand.ExecuteNonQuery();
//command.ExecuteNonQuery();
Jaish MathewsPosted Apr 14, 2010, 7:56 AM
Need ur DB details as error relates to DB
Ankit RathorePosted Apr 14, 2010, 2:54 AM
Here is my whole code ............
================================
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;
public partial class _Default : System.Web.UI.Page
{
cConnect1 objcConnect = new cConnect1();
int i = 0;
protected void Page_Load(object sender, EventArgs e)
{
objcConnect.getdata();
DisplayData();
}
private void DisplayData()
{
txtEmpId.Text = objcConnect.dset.Tables["Emp"].Rows[i]["ID"].ToString();
txtName.Text = objcConnect.dset.Tables["Emp"].Rows[i]["Name"].ToString();
txtDesignation.Text = objcConnect.dset.Tables["Emp"].Rows[i]["Designation"].ToString();
txtTaddress.Text = objcConnect.dset.Tables["Emp"].Rows[i]["TempAddress"].ToString();
txtPAddress.Text = objcConnect.dset.Tables["Emp"].Rows[i]["PermanentAddress"].ToString();
txtSalary.Text = objcConnect.dset.Tables["Emp"].Rows[i]["Salary"].ToString();
txtContact.Text = objcConnect.dset.Tables["Emp"].Rows[i]["Contact"].ToString();
}
protected void btnNext_Click(object sender, EventArgs e)
{
i++;
DisplayData();
}
protected void btnPrevious_Click(object sender, EventArgs e)
{
if (IsPostBack == false)
i--;
DisplayData();
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtEmpId.Text = "";
txtName.Text = "";
txtDesignation.Text = "";
txtPAddress.Text = "";
txtSalary.Text = "";
txtTaddress.Text = "";
txtContact.Text = "";
}
protected void btnInsert_Click(object sender, EventArgs e)
{
//if (IsPostBack == false)
InsertRecord();
}
private void InsertRecord()
{
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());
}
}
}
Ankit RathorePosted Apr 14, 2010, 2:40 AM
in ID :- A101, A102,
Trying to insert :- A103 or E001
Jaish MathewsPosted Apr 14, 2010, 2:21 AM
As you may already aware, a filed marked as primary key can not accept dulicate values