![](/img/trans.png)
[英]Failed to convert parameter value from a String to a Int32 while executing stored procedure
[英]Failed to convert parameter value from a String to a Int32 and my stored procedure is with the same parameters
我知道這個問題已經被大量提及,但是問題卻浪費了我的時間,而且在我的存儲過程中,參數是相同的,我不知道為什么會這樣。 我希望你能幫幫我
protected void Button1_Click(object sender, EventArgs e)
{
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sp_InfoPacient";
cmd.Connection = cn;
SqlParameter pd = new SqlParameter("@PacientId",SqlDbType.Int);
pd.Value = DropDownList1.Text;
cmd.Parameters.Add(pd);
SqlParameter pname = new SqlParameter("@EmriPacientit",SqlDbType.VarChar,20);
pname.Value = TextBox12.Text;
cmd.Parameters.Add(pname);
SqlParameter age = new SqlParameter("@Mosha",SqlDbType.Int);
age.Value = TextBox13.Text;
cmd.Parameters.Add(age);
SqlParameter dep = new SqlParameter("@Departamenti", SqlDbType.VarChar, 20);
dep.Value = TextBox14.Text;
cmd.Parameters.Add(dep);
SqlParameter doct = new SqlParameter("@DrPersonal", SqlDbType.VarChar, 20);
doct.Value = TextBox15.Text;
cmd.Parameters.Add(doct);
cmd.ExecuteNonQuery();
}
在我看來,它可以歸結為以下兩行:
> SqlParameter pd = new SqlParameter("@PacientId",SqlDbType.Int);
> pd.Value = DropDownList1.Text;
在第一行中,您要告訴命令參數“ PacientId”的類型為int
。
在第二行中,為參數分配DropDownList1.Text
的值,該值將為string
類型。
若要解決此問題,您可能需要Convert.ToInt32(DropDownList1.Text)
以確保您設置的參數值為整數。
正如Hogan指出的那樣,如果Text值不是整數,則存在潛在的問題-例如,如果您具有值“ 1”,“ 2”,“ 3”和“ Potato”。 您不能將“ Panini”解析為一個Int。 當您不確定要轉換的輸入時,請使用Int.TryParse。 MSDN文檔對此進行了很好的描述。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.