faramarz_s
جمعه 28 شهریور 1382, 22: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();
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();