问题在从LINQ查询和显示到文本框中获取数据时发生的问题 [英] Problem Occuring at the time of fetching data from the LINQ query and Display into textbox..
本文介绍了问题在从LINQ查询和显示到文本框中获取数据时发生的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个网络表单。在这个表单中我放了一个文本框,下拉列表和一个网格视图。在gridview中,模板字段为Linkbutton(Command Name =view)。因此,当我单击查看按钮时,所选数据将显示在文本框和下拉列表中。
我在gridview行命令上编写以下代码事件但仍然显示错误。 。
i have a one web form.in this form i put one text box ,drop down list and one grid view. In gridview there is template field as a Linkbutton (Command Name="view"). so when I click On view button then selected data will be display on text box and dropdown list..
Iam writing following code on gridview row command Event But Still it Display Error. .
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
db.Connection.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Cn"].ConnectionString;
if (e.CommandName == "view")
{
int StateId = Convert.ToInt32(e.CommandArgument);
HiddenField1.Value = StateId.ToString();
var state =
(
from s in db.tblStates
where (s.Stateid == Convert.ToInt32(e.CommandArgument))
select new
{
s.StateName,
s.Countryid
}
).FirstOrDefault();
if (state != null)
{
TextBox1.Text = state.StateName;
DropDownList1.SelectedValue = state.Countryid;
}
}
}
显示Follwing错误..
It Display Follwing Error..
System.InvalidCastException: Unable to cast object of type 'System.Int32' to type 'System.String'.
Source Error:
Line 78: int StateId = Convert.ToInt32(e.CommandArgument);
Line 79: HiddenField1.Value = StateId.ToString();
Line 80: var state =
Line 81: (
Line 82: from s in db.tblStates
Thanx
Thanx
推荐答案
问题在于在
(s.Stateid == Convert.ToInt32(e.CommandArgument))
linq不支持在获取数据时转移到int
试试这个,
where(s.Stateid == StateId)
problem lies in
(s.Stateid == Convert.ToInt32(e.CommandArgument))
linq does not support conveting to int while fetching data
try this,
where (s.Stateid ==StateId)
我想先检查你的命令参数值,它将是数字,以及第二个解决方案
I think first check your command argument value,it will be numberic,and 2nd above solution
这篇关于问题在从LINQ查询和显示到文本框中获取数据时发生的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文