未设置对象引用/无法将类型为“System.Web.UI.WebControls.Label”的对象强制转换为“System.IConvertible”。 [英] Object reference not set / Unable to cast object of type 'System.Web.UI.WebControls.Label' to type 'System.IConvertible'.
本文介绍了未设置对象引用/无法将类型为“System.Web.UI.WebControls.Label”的对象强制转换为“System.IConvertible”。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在int empId = Convert.ToInt32(labelId.Text)上得到此错误;
无法将System.Web.UI.WebControls.Label类型的对象强制转换为
''System.IConvertible''。
protected void LinkButton1_Click( object sender,EventArgs e)
{
try
{
int index = 0 ;
LinkButton lb =(LinkButton)sender;
GridViewRow row =(GridViewRow)lb.NamingContainer;
if (row!= null )
{
index = row.RowIndex;
}
var labelId =(Label)NameDisplay.Rows [index] .FindControl( labelGetId);
labelId.Text = HiddenFieldID.Value;
int empId = Convert.ToInt32(labelId.Text);
使用( var db = new knowitCVdbEntities())
{
var theEmpl =( from
p 在 db.EMPLOYEES
其中 p.employee_id == empId
选择 p).FirstOrDefault();
if (theEmpl!= null )
{
theEmpl.firstname = TextBoxFirstName.Text;
theEmpl.lastname = TextBoxLastName.Text;
}
}
}
catch (例外x)
{
LabelError.Visible = true ;
LabelError.Text = x.Message;
}
}
}
}
解决方案
< blockquote>为什么不直接从HiddenFieldID.Value转换empID而不是将标签设置为该值然后再将其拉回?看起来好像很多,无所事事......
i get this error on int empId = Convert.ToInt32(labelId.Text);
Unable to cast object of type ''System.Web.UI.WebControls.Label'' to type
''System.IConvertible''.
protected void LinkButton1_Click(object sender, EventArgs e)
{
try
{
int index = 0;
LinkButton lb = (LinkButton) sender;
GridViewRow row = (GridViewRow) lb.NamingContainer;
if (row != null)
{
index = row.RowIndex;
}
var labelId = (Label) NameDisplay.Rows[index].FindControl("labelGetId");
labelId.Text = HiddenFieldID.Value;
int empId = Convert.ToInt32(labelId.Text);
using (var db = new knowitCVdbEntities())
{
var theEmpl = (from
p in db.EMPLOYEES
where p.employee_id == empId
select p).FirstOrDefault();
if (theEmpl != null)
{
theEmpl.firstname = TextBoxFirstName.Text;
theEmpl.lastname = TextBoxLastName.Text;
}
}
}
catch (Exception x)
{
LabelError.Visible = true;
LabelError.Text = x.Message;
}
}
}
}
解决方案
Why not just convert the empID from HiddenFieldID.Value instead of setting the label to that value then pulling it back out again? Seems like a lot of round-about for nothing...
这篇关于未设置对象引用/无法将类型为“System.Web.UI.WebControls.Label”的对象强制转换为“System.IConvertible”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文