PDA

View Full Version : عدم اینسرت داده



faramarz_s
جمعه 28 شهریور 1382, 21:51 عصر
کد زیر بدرستی داده های قبلی موجود در بانک را در دیتا گرید نمایش می دهد اما داده ای اینسرت نمی کند!

private void AddAuthor_Click(object sender, System.EventArgs e)
{
// Check that four of the input values are not empty. If any of them
// is empty, show a message to the user and rebind the DataGrid.
/*if (au_id.Value == "" || au_fname.Value == "" ||
au_lname.Value == "" || phone.Value == "")
{
Message.InnerHtml = "ERROR: Null values not allowed for" +
" tblSupplies ID, Name or DeviceType";
Message.Style["color"] = "red";
BindGrid();
return;
}*/
// Build a SQL Insert statement string for all the input-form
// field values.
SqlConnection myConnection = new SqlConnection("server=(local);uid=sa;pwd=yaali;database=MDB");
String insertCmd = "insert into tblSupplies values (name," +
" ,model,DeviceType,GetType,EnterDate,FixRecord,Pric e,Force,Location,Answerable)";

SqlCommand myCommand = new SqlCommand(insertCmd, myConnection);

myCommand.Parameters.Add(new SqlParameter("@name",
SqlDbType.NChar, 30));
myCommand.Parameters["@name"].Value = name.Value;
myCommand.Parameters.Add(new SqlParameter("@Model",
SqlDbType.NChar, 10));
myCommand.Parameters["@Model"].Value = Model.Value;
myCommand.Parameters.Add(new SqlParameter("@DeviceType",
SqlDbType.NChar, 10));
myCommand.Parameters["@DeviceType"].Value = DeviceType.Value;
myCommand.Parameters.Add(new SqlParameter("@GetType",
SqlDbType.NChar, 10));
myCommand.Parameters["@GetType"].Value = GetType1.Value;
myCommand.Parameters.Add(new SqlParameter("@EnterDate",
SqlDbType.NChar, 10));
myCommand.Parameters["@EnterDate"].Value = EnterDate.Value;
myCommand.Parameters.Add(new SqlParameter("@FixRecord",
SqlDbType.NVarChar, 50));
myCommand.Parameters["@FixRecord"].Value = FixRecord.Value;
myCommand.Parameters.Add(new SqlParameter("@Price",
SqlDbType.BigInt, 8));
myCommand.Parameters["@Price"].Value = Price.Value;
myCommand.Parameters.Add(new SqlParameter("@Force",
SqlDbType.TinyInt,1));
myCommand.Parameters["@Force"].Value = Force.Value;
myCommand.Parameters.Add(new SqlParameter("@Location",
SqlDbType.NChar,10));
myCommand.Parameters["@Location"].Value = Location.Value;
myCommand.Parameters.Add(new SqlParameter("@Answerable",
SqlDbType.NChar,10));
myCommand.Parameters["@Answerable"].Value =Answerable.Value;

myCommand.Connection.Open();
myCommand.ExecuteNonQuery();

/*try
{
myCommand.ExecuteNonQuery();
Message.InnerHtml = "<b>Record Added</b><br>" + insertCmd;
}
catch (SqlException e)
{
if (e.Number == 2627)
Message.InnerHtml = "ERROR: A record already exists with" +
" the same primary key";
else
Message.InnerHtml = "ERROR: Could not add record, please " +
" ensure the fields are correctly filled out";
Message.Style["color"] = "red";
}*/
myCommand.Connection.Close();

BindGrid();

faramarz_s
شنبه 29 شهریور 1382, 09:18 صبح
به نظر شما مشکل چیست؟

Vahid_Nasiri
شنبه 29 شهریور 1382, 14:49 عصر
جایی که داده ها را bigInt‌ و امثال این تعریف کرده اید آیا نباید Convert.ToInt32 را انجام دهید؟

faramarz_s
یک شنبه 30 شهریور 1382, 09:56 صبح
این را اصلاح کردم درست شد اما الان 2 مشکل هست:
1-رکوردی با پرایمری کی تکراری به آن نمی دهم اما پیغام خطای آنرا دریافت میکنم.
وقتی که پرایمری تکراری می دهم این پیغام را می دهد:

2-در مورد تاینی اینت چطور کانورت می خواهد؟
Server Error in '/Merat' Application.
--------------------------------------------------------------------------------

Violation of PRIMARY KEY constraint 'PK_tblSupplies'. Cannot insert duplicate key in object 'tblSupplies'. The statement has been terminated.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Violation of PRIMARY KEY constraint 'PK_tblSupplies'. Cannot insert duplicate key in object 'tblSupplies'. The statement has been terminated.

Source Error: