无法将参数值从Label转换为Int32 [英] Failed to convert parameter value from a Label to a Int32

查看:126
本文介绍了无法将参数值从Label转换为Int32的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决这个问题?



在这里,我给出了编码。当debegger在ExcuteNonQuery()上执行时,我收到错误,例如无法将参数值从Label转换为Int32;



 protected void utnclcicked (object sender,ImageClickEventArgs e)
{
for(int i = 0; i< = C1GridView1.Rows.Count-1; i ++)
{

TextBox tqty1 =(TextBox)C1GridView1.Rows [i] .Cells [7] .FindControl(num);


TextBox l1 =(TextBox)GridView1.Rows [i] .Cells [0] .FindControl(txtnote);
标签m1 =(标签)GridView1.Rows [i] .Cells [1] .FindControl(lblmaxqty);
标签m2 =(标签)GridView1.Rows [i] .Cells [2] .FindControl(lblminqty);
标签m3 =(标签)GridView1.Rows [i] .Cells [3] .FindControl(lblivtfid);
标签m4 =(标签)GridView1.Rows [i] .Cells [4] .FindControl(code);
标签m5 =(标签)GridView1.Rows [i] .Cells [6] .FindControl(descript);
标签m6 =(标签)GridView1.Rows [i] .Cells [8] .FindControl(lblprice);
标签m7 =(标签)GridView1.Rows [i] .Cells [8] .FindControl(lblmfg);

int s1 = Convert.ToInt32(tqty1.Text.ToString()。Trim());
string s2 = lisname.Text.ToString()。Trim();
int s3 = Convert.ToInt32(maxqty.Text.ToString()。Trim());
int s4 = Convert.ToInt32(minqty.Text.ToString()。Trim());
int s5 = Convert.ToInt32(pid.Text.ToString()。Trim());
string s6 = lblitem.Text.ToString()。Trim();
string s7 = des.Text.ToString()。Trim();
decimal s8 = Convert.ToDecimal(price1.Text.ToString()。Trim());

string s9 = mfg1.Text.ToString()。Trim();

string s10 = Session [password]。ToString();
string s11 = Session [LoggedIN]。ToString();
SqlConnection conlist = new SqlConnection(constring);
conlist.Open();

SqlCommand cmd = new SqlCommand(sp_testsp,conlist);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(@ t1,SqlDbType.NVarChar).Value = s1
cmd.Parameters.Add(@ t2,SqlDbType.Int).Value = s2
cmd.Parameters.Add(@ t3,SqlDbType.Int).Value = s3
cmd.Parameters.Add(@ t4,SqlDbType.Int).Value = s4
cmd.Parameters。添加(@ t5,SqlDbType.NVarChar).Value = s5
cmd.Parameters.Add(@ t6,SqlDbType.NVarChar).Value = s6
cmd.Parameters.Add(@ t7,SqlDbType.Int).Value = s7
cmd.Parameters.Add(@ t8,SqlDbType.Decimal).Value = s8
cmd.Parameters.Add(@ t9,SqlDbType .NVarChar)。Value = s9
cmd.Parameters.Add(@ t10,SqlDbType.NVarChar).Value = s10
cmd.Parameters.Add(@ t11,SqlDbType.NVarChar)。值= s11
cmd.ExecuteNonQuery();
conlist.Close();


}
}







i am从gridview的单元格获取值并从lablel,text值转换为int类型和字符串类型然后我将这些值作为参数传送给存储的prroceduresp_testsp..



Everthing工作正常。将标签和文本转换为int和string时,没有错误。但是,当光标(调试器)来到cmd.ExecuteNonQuery()时,我收到错误如下:



无法将参数值从Label转换为Int32。 />


如何解决这个问题?为什么它说无法将参数值从标签转换为Int32。需要帮助。

解决方案

实际上我认为错误更像是无法从字符串转换为int,而不是标签。



问题是S7,你将它存储在一个字符串中,但在你的查询参数中你将它设置为一个Int。



以下是两行,注意s7如何存储在字符串中,并参数化为int。



  string  s7 = des.Text.ToString()。Trim(); 
cmd.Parameters.Add( @ t7,SqlDbType.Int).Value = s7;


Ron Bayer已经指出过的问题。所以尝试转换它就像你已经为s3,s4,s5做的那样。



 command.Parameters.Add(@ t7,SqlDbType.Int).Value = Convert.ToInt32(s7); 





希望这有助于......


另一种处理null并将字符串转换为int的方法。 />
int s7 = 0;

if(!string.isnullorempty(des.Text))

s7 = convert.toint32(des.Text.trim ());

hi, how to solve this?

here, i give coding. i am getting error like failed to convert parameter value from a Label to a Int32, when debegger excutes on ExcuteNonQuery();

protected void utnclcicked(object sender, ImageClickEventArgs e)
{
for (int i = 0; i <= C1GridView1.Rows.Count-1; i++)
{

TextBox tqty1 = (TextBox)C1GridView1.Rows[i].Cells[7].FindControl("num");


TextBox l1= (TextBox)GridView1.Rows[i].Cells[0].FindControl("txtnote");
Label m1= (Label)GridView1.Rows[i].Cells[1].FindControl("lblmaxqty");
Label m2= (Label)GridView1.Rows[i].Cells[2].FindControl("lblminqty");
Label m3= (Label)GridView1.Rows[i].Cells[3].FindControl("lblivtfid");
Label m4= (Label)GridView1.Rows[i].Cells[4].FindControl("code");
Label m5= (Label)GridView1.Rows[i].Cells[6].FindControl("descript");
Label m6= (Label)GridView1.Rows[i].Cells[8].FindControl("lblprice");
Label m7= (Label)GridView1.Rows[i].Cells[8].FindControl("lblmfg");

int s1= Convert.ToInt32(tqty1.Text.ToString().Trim());
string s2= lisname.Text.ToString().Trim();
int s3= Convert.ToInt32( maxqty.Text.ToString().Trim());
int s4= Convert.ToInt32(minqty.Text.ToString().Trim());
int s5= Convert.ToInt32(pid.Text.ToString().Trim());
string s6= lblitem.Text.ToString().Trim();
string s7= des.Text.ToString().Trim();
decimal s8 = Convert.ToDecimal(price1.Text.ToString().Trim());

string s9= mfg1.Text.ToString().Trim();

string s10= Session["password"].ToString();
string s11= Session["LoggedIN"].ToString();
SqlConnection conlist = new SqlConnection(constring);
conlist.Open();

SqlCommand cmd = new SqlCommand("sp_testsp", conlist);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@t1", SqlDbType.NVarChar).Value = s1
cmd.Parameters.Add("@t2", SqlDbType.Int).Value = s2
cmd.Parameters.Add("@t3", SqlDbType.Int).Value = s3
cmd.Parameters.Add("@t4", SqlDbType.Int).Value = s4
cmd.Parameters.Add("@t5", SqlDbType.NVarChar).Value = s5
cmd.Parameters.Add("@t6", SqlDbType.NVarChar).Value = s6
cmd.Parameters.Add("@t7", SqlDbType.Int).Value = s7
cmd.Parameters.Add("@t8", SqlDbType.Decimal).Value = s8
cmd.Parameters.Add("@t9", SqlDbType.NVarChar).Value = s9
cmd.Parameters.Add("@t10", SqlDbType.NVarChar).Value = s10
cmd.Parameters.Add("@t11", SqlDbType.NVarChar).Value = s11
cmd.ExecuteNonQuery();
conlist.Close();


}
}




i am getting value from cells of gridview and converting from lablel, text value into int type and string type and then i am sending those values as parameteres for stored prrocedure "sp_testsp"..

Everthing works fine. while converting label and text to int and string, no error. but, when cursor(debugger) comes to cmd.ExecuteNonQuery() i am getting error follow:

Failed to convert parameter value from a Label to a Int32.

How to solve this ? why It says that failed to convert parameter value from a label to Int32. Help needed.

解决方案

Actually I think the error says something more like "unable to convert from string to int", not label.

The problem is S7, you are storing it in a string, but in your query parameters you have it set as an Int.

Here are the two lines, notice how s7 is stored in a string, and parameterized as an int.

string s7= des.Text.ToString().Trim();
cmd.Parameters.Add("@t7", SqlDbType.Int).Value = s7;


The Issue Ron Bayer already have pointed out. So try converting it as you are already doing for s3,s4,s5 .

command.Parameters.Add("@t7", SqlDbType.Int).Value = Convert.ToInt32(s7);



Hope this helps...


Another way to handle null and convert string to int.
int s7=0;
if(!string.isnullorempty(des.Text))
s7=convert.toint32(des.Text.trim());


这篇关于无法将参数值从Label转换为Int32的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆