无法将“System.String”类型的对象强制转换为“System.Data.DataTable”类型 [英] Unable to cast object of type 'System.String' to type 'System.Data.DataTable'

查看:106
本文介绍了无法将“System.String”类型的对象强制转换为“System.Data.DataTable”类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过GridView中的会话更新我的行。只有GridView中的Row更新操作才会引发服务器错误。我项目中的其他操作运行正常。当我添加以下代码来更新索引时,它会引发服务器错误。请指导。



服务器错误 -



'/'应用程序中的服务器错误。



无法将'System.String'类型的对象强制转换为'System.Data.DataTable'类型。



描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的起源的更多信息。



异常详细信息:System.InvalidCastException:无法转换类型为System的对象.String'输入'System.Data.DataTable'。



来源错误:



第107行:con.Open();

第108行:
$ b $第109行:DataTable dt =(DataTable)会话[名称];

第110行:

第111行://更新值。





我的aspx.cs代码为更新gridview中的行:





protected void GridView1_RowUpdating(object sender,GridViewUpdateEventArgs e)

{

SqlConnection con = new SqlConnection(strConnString);

con.Open();



DataTable dt =(DataTable )Session [name];



//更新值。

GridViewRow row = GridView1.Rows [e.RowIndex];

// int id = Convert.ToInt32(Label。[e.RowIndex] .Values [id]。ToString());

dt.Rows [row.DataItemIndex] [name] =((TextBox)(row.Cells [1] .Controls [0]))。文字;

dt .Rows [row.DataItemIndex] [role] =((TextBox)(row.Cells [2] .Controls [0]))。Text;

dt.Rows [row.DataItemIndex] [gender] =((TextBox)(row.Cells [3] .Controls [0]))。文字;

dt.Rows [row.DataItemIndex] [exp] =( (TextBox)(row.Cells [4] .Controls [0]))。文本;

dt.Rows [row.DataItemIndex] [join] =((TextBox)(row.Cells [5] .Controls [0]))。文本;

dt.Rows [row.DataItemIndex] [report] =((TextBox)(row.Cells [6] .Controls [0 ]))。文字;

dt.Rows [row.DataItemIndex] [email] =((TextBox)(row.Cells [7] .Controls [0]))。Text;

dt.Rows [row.DataItemIndex] [mobile] =((TextBox)(row.Cells [8] .Controls [0]))。文字;

dt.Rows [row.DataItemIndex] [birth] =((TextBox)(r ow.Cells [9] .Controls [0])。文本;

dt.Rows [row.DataItemIndex] [address] =((TextBox)(row.Cells [10]。控件[0]))。文本;



SqlCommand com = new SqlCommand(update_employee,con);

com.CommandType = CommandType.StoredProcedure;

com.Connection = con;

com.Parameters.Add(@ id,SqlDbType.Int).Value = id;

com.Parameters.Add(@ name,SqlDbType.VarChar,50).Value = name;

com.Parameters.Add(@ address,SqlDbType.VarChar,50 ).Value =地址;

com.Parameters.Add(@ email,SqlDbType.VarChar,50).Value = email;

com.Parameters.Add( @mobile,SqlDbType.VarChar,50).Value = mobile;

com.Parameters.Add(@ gender,SqlDbType.VarChar,50).Value = gender;

com.Parameters.Add(@ role,SqlDbType.VarChar,50).Value = role;

com.Parameters.Add(@ birth,SqlDbType.VarChar,50 ).value的= b诞生;

com.Parameters.Add(@ join,SqlDbType.VarChar,50).Value = join;

com.Parameters.Add(@ exp ,SqlDbType.VarChar,50).Value = exp;

com.Parameters.Add(@ report,SqlDbType.VarChar,50).Value = report;

com.ExecuteNonQuery();

com.Dispose();

con.Close();

GridView1.EditIndex = -1;

bindgrid();

}

I am attempting to update my rows through session in GridView. Only the Row update operation in GridView throws server error. Other operations in my project works fine. When I add this following code to update the index, it throws server error. Please guide.

Server Error -

Server Error in '/' Application.

Unable to cast object of type 'System.String' to type 'System.Data.DataTable'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Data.DataTable'.

Source Error:

Line 107: con.Open();
Line 108:
Line 109: DataTable dt = (DataTable)Session["name"];
Line 110:
Line 111: //Update the values.


My aspx.cs code to update rows in gridview:


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();

DataTable dt = (DataTable)Session["name"];

//Update the values.
GridViewRow row = GridView1.Rows[e.RowIndex];
//int id = Convert.ToInt32(Label.[e.RowIndex].Values["id"].ToString());
dt.Rows[row.DataItemIndex]["name"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["role"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["gender"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["exp"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["join"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["report"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["email"] = ((TextBox)(row.Cells[7].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["mobile"] = ((TextBox)(row.Cells[8].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["birth"] = ((TextBox)(row.Cells[9].Controls[0])).Text;
dt.Rows[row.DataItemIndex]["address"] = ((TextBox)(row.Cells[10].Controls[0])).Text;

SqlCommand com = new SqlCommand("update_employee", con);
com.CommandType = CommandType.StoredProcedure;
com.Connection = con;
com.Parameters.Add("@id", SqlDbType.Int).Value = id;
com.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = name;
com.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = address;
com.Parameters.Add("@email", SqlDbType.VarChar, 50).Value = email;
com.Parameters.Add("@mobile", SqlDbType.VarChar, 50).Value = mobile;
com.Parameters.Add("@gender", SqlDbType.VarChar, 50).Value = gender;
com.Parameters.Add("@role", SqlDbType.VarChar, 50).Value = role;
com.Parameters.Add("@birth", SqlDbType.VarChar, 50).Value = birth;
com.Parameters.Add("@join", SqlDbType.VarChar, 50).Value = join;
com.Parameters.Add("@exp", SqlDbType.VarChar, 50).Value = exp;
com.Parameters.Add("@report", SqlDbType.VarChar, 50).Value = report;
com.ExecuteNonQuery();
com.Dispose();
con.Close();
GridView1.EditIndex = -1;
bindgrid();
}

推荐答案

您无法将字符串类型转换为datatable。 Session [name]不是数据表。
You cannot convert a string type to datatable. Session[name] is not a datatable.


你需要开始查看设置Session变量的代码:要么完全是错误的变量,要么是错误的变量!



猜测,会话[名称] 保存您的用户名,而不是您期望的数据表。



但就个人而言,我不会在会话中存储数据表 - 它们可能会占用大量空间,如果同时获得许多用户,当内存运行时,事情就会开始停滞不前出。我会存储ID或其他任何内容来检索数据,并在需要时再次获取它。
You need to start looking at the code which sets the Session variable: either that or it's teh wrong variable completely!

At a guess, Session["Name"] holds your user name, not the datatable you expect.

But personally, I wouldn't store datatables in the Session - they can take a lot of space, and if you get many users concurrently, things can start to grind to a halt as the memory runs out. I'd store the ID's or whatever to retrieve the data and fetch it again when I needed it.


这篇关于无法将“System.String”类型的对象强制转换为“System.Data.DataTable”类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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