Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help me GridView c# ASp.net 2.0

Status
Not open for further replies.

090123123

Programmer
Mar 17, 2009
2
please I want Delete and update from gridview using code but not work and then run msg error : Insert/Update Error:INSERT statement conflicted with COLUMN CHECK constraint 'CK__Customer___Custo__03317E3D'. The conflict occurred in database 'MerryMeeting', table 'Customer_Details', column 'Customer_id'.
The statement has been terminated.


this is my code :


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
SqlConnection myconection;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}

}
private void BindGridView()
{
DataTable dt = new DataTable();
SqlConnection myconection = new SqlConnection("Persist security info=false;User=sa;Password=123;Initial Catalog=MerryMeeting;Data Source=FBI-4D11DAAB9E2");
try
{
myconection.Open();
string sqlStatement = "SELECT * FROM Customer_Details";
SqlCommand cmd = new SqlCommand(sqlStatement, myconection);
SqlDataAdapter sqlDa = new SqlDataAdapter(cmd);

sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
MyGridView.DataSource = dt;
MyGridView.DataBind();
}
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
myconection.Close();
}

}



private void UpdateOrAddNewRecord(string id, string Name, string Address, string Number, string Email, string Contact_Person, bool isUpdate)
{
SqlConnection connection = new SqlConnection("Persist security info=false;User=sa;Password=123;Initial Catalog=MerryMeeting;Data Source=FBI-4D11DAAB9E2");
string sqlStatement = string.Empty;

if (!isUpdate)
{
sqlStatement = "INSERT INTO Customer_Details"+
"(Customer_id,Customer_Name,Customer_Address,Customer_Number,Customer_Email,Customer_Contact_Person)" +
"VALUES (@Customer_id,@Customer_Name,@Customer_Address,@Customer_Number,@Customer_Email,@Customer_Contact_Person)";
}
else
{
sqlStatement = "UPDATE Customer_Details" +
"SET Customer_Name = @Customer_Name,Customer_Address = @Customer_Address," + "Customer_Number = @Customer_Number,Customer_Email = @Customer_Email,Customer_Contact_Person = @Customer_Contact_Person" +
"WHERE Customer_id = @Customer_id,";
}
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, connection);
cmd.Parameters.AddWithValue("@Customer_id", id);
cmd.Parameters.AddWithValue("@Customer_Name", Name);
cmd.Parameters.AddWithValue("@Customer_Address", Address);
cmd.Parameters.AddWithValue("@Customer_Number", Number);
cmd.Parameters.AddWithValue("@Customer_Email", Email);
cmd.Parameters.AddWithValue("@Customer_Contact_Person",Contact_Person);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert/Update Error:";
msg += ex.Message;
throw new Exception(msg);

}
finally
{
connection.Close();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
UpdateOrAddNewRecord(TextBox1.Text, TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, false);
BindGridView();

}

protected void MyGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
MyGridView.EditIndex = e.NewEditIndex;
BindGridView();
}

protected void MyGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
MyGridView.EditIndex = -1;
BindGridView();
}

protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

string id = MyGridView.Rows[e.RowIndex].Cells[0].Text;
string Name = ((TextBox)MyGridView.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Address = ((TextBox)MyGridView.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Number = ((TextBox)MyGridView.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string Email = ((TextBox)MyGridView.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string Contact_Person = ((TextBox)MyGridView.Rows[e.RowIndex].Cells[5].Controls[0]).Text;

UpdateOrAddNewRecord(id, Name, Address, Number, Email, Contact_Person, true);
MyGridView.EditIndex = -1;
BindGridView();
}

private void DeleteRecord(string ID)
{
SqlConnection connection = new SqlConnection("Persist security info=false;User=sa;Password=123;Initial Catalog=MerryMeeting;Data Source=FBI-4D11DAAB9E2");
string sqlStatement = "DELETE FROM Customer_Details WHERE Customer_id = @Customer_id";
try
{
connection.Open();
SqlCommand cmd = new SqlCommand(sqlStatement, myconection);
cmd.Parameters.AddWithValue("@Customer_id", ID);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Deletion Error:";
msg += ex.Message;
throw new Exception(msg);

}
finally
{
connection.Close();
}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = MyGridView.Rows[e.RowIndex].Cells[0].Text;
DeleteRecord(id);

}

}

help me !! Thanks all
 
it's a database error. you are trying to insert a child record without a parent. in this case it looks like you are trying to insert details for a customer that does not exist.

step through the code to ensure the customer id is correct.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top