我收到nvarchar转换为int的错误。我也试过解析,但它没有解决我的问题。那么我该如何解决这个问题呢?请给我解决方案。
存储过程中的代码:
ALTER PROCEDURE dbo.IDMaker
(@Name varchar(50) = '',
@Address varchar(50) = '',
@Contact int,
@EmergencyContact int,
@BloodGroup varchar(50) = '',
@Disease varchar(50) = '',
@Drug varchar(50) = '',
@Operation varchar(50),
@ID int = 0)
AS
BEGIN
IF(@Operation = 1)
BEGIN
INSERT INTO IDRegis(Name, Address, Contact, EmergencyContact, BloodGroup, Disease, Drug)
VALUES (@Name, @Address, @Contact, @EmergencyContact, @BloodGroup, @Disease, @Drug)
RETURN
END
Windows窗体中的代码:
//int contact, emcontact;
//if (!Int32.TryParse(txtphone.Text, out contact))
//{
// MessageBox.Show("Enter Valid number...");
// return;
//}
//if (!Int32.TryParse(txtemphone.Text, out emcontact))
//{
// MessageBox.Show("Enter Valid number...");
// return;
//}
if (objmyclass.con.State != ConnectionState.Open)
{
objmyclass.con.Open();
}
SqlCommand cmd = new SqlCommand("IDMaker", objmyclass.con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", txtname.Text);
cmd.Parameters.AddWithValue("@Address", txtadd.Text);
cmd.Parameters.AddWithValue("@Contact", txtphone.Text);
cmd.Parameters.AddWithValue("@EmergencyContact",txtemphone.Text);
cmd.Parameters.AddWithValue("@BloodGroup", txtbg.Text);
cmd.Parameters.AddWithValue("@Disease", txted.Text);
cmd.Parameters.AddWithValue("@Drug", txtdrug.Text);
cmd.Parameters.AddWithValue("@Operation", 1);
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Record Successfully Inserted....");
}