代码如下:
private void Button1_Click(object sender, System.EventArgs e)
{
int Id;
string SqlConn=new gwbn.sqlconn().sqlcon();
SqlConnection Conn=new SqlConnection(SqlConn);
SqlCommand Cmd=new SqlCommand("select max(listnum) from number ",Conn);
Conn.Open();
Id=(int)Cmd.ExecuteScalar();
Conn.Close();
Response.Write(Id.ToString());
}
“/gwbn”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 54: SqlCommand Cmd=new SqlCommand("select max(listnum) from number ",Conn);
行 55: Conn.Open();
行 56: Id=(int)Cmd.ExecuteScalar();//报错
行 57: Conn.Close();
行 58: Response.Write(Id.ToString());
源文件: c:\inetpub\wwwroot\gwbn\webform2.aspx.cs 行: 56 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
gwbn.WebForm2.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\gwbn\webform2.aspx.cs:56
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
就是不知道有什么问题--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本
private void Button1_Click(object sender, System.EventArgs e)
{
int Id;
string SqlConn=new gwbn.sqlconn().sqlcon();
SqlConnection Conn=new SqlConnection(SqlConn);
SqlCommand Cmd=new SqlCommand("select max(listnum) from number ",Conn);
Conn.Open();
Id=(int)Cmd.ExecuteScalar();
Conn.Close();
Response.Write(Id.ToString());
}
“/gwbn”应用程序中的服务器错误。
--------------------------------------------------------------------------------指定的转换无效。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.InvalidCastException: 指定的转换无效。源错误:
行 54: SqlCommand Cmd=new SqlCommand("select max(listnum) from number ",Conn);
行 55: Conn.Open();
行 56: Id=(int)Cmd.ExecuteScalar();//报错
行 57: Conn.Close();
行 58: Response.Write(Id.ToString());
源文件: c:\inetpub\wwwroot\gwbn\webform2.aspx.cs 行: 56 堆栈跟踪:
[InvalidCastException: 指定的转换无效。]
gwbn.WebForm2.Button1_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\gwbn\webform2.aspx.cs:56
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
就是不知道有什么问题--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本
改为
SqlCommand Cmd=new SqlCommand("select count(listnum) from number ",Conn);
就能正常执行
select max(listnum) from number 在SQL里能正常执行啊!!!
count()是返回记录数,是int,所以不会出错了
改成
Id=int.Parse(Cmd.ExecuteScalar());
试试