System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。
using System;using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class yd : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userinfo"] == null)
{
Response.Write("<script>alert('我是订餐页面!未授权用户不能访问');location='dl.aspx'</script>");
}
else
{
return ;
}
}
protected void Button1_Click(object sender, EventArgs e)
{ try
{
hyClass zc = new hyClass();
zc = (hyClass)Session["userinfo"];
string dcsj = TextBox1.Text.ToString();
string dcsd = DropDownList2.Text.ToString();
string czm = DropDownList1.Text.ToString();
string sqltemp = "select czid,czm,czrs,czxx from cz where czm='" + czm.ToString() + "'";
sql sc = new sql();
SqlDataReader sr = sc.reader(sqltemp);
string sqltem = "select dcid,dcsj,czid,hyid,dcsd from hy where czid='" + sr["czid"].ToString() + "' and dcsj='" + dcsj + "'and dcsd='" + dcsd + "'";
sql sac = new sql();
SqlDataReader sr1 = sac.reader(sqltem);
if (sr1.Read())
{
Response.Write("<script>alert('已有人预定!');</script>");
}
else
{
string sqltem1 = "insert into dc( dcsj,czid, hyid,dcsd) values ( '" + dcsj + "','" + sr["czid"].ToString() + "','" + zc.Hyid.ToString() + "','" + dcsd + "')";
sql sxc = new sql();
int t = sxc.nonquery(sqltem);
if (t == 1)
{
Response.Write("<script>alert('添加成功');location='wjgl.aspx'</script>");
}
else
{
Response.Write("<script>alert('添加失败!');</script>");
}
}
}
catch (Exception ex)
{
Response.Write(ex);
}
}
}
报错信息
System.InvalidOperationException: 在没有任何数据时进行无效的读取尝试。 在 System.Data.SqlClient.SqlDataReader.GetValue(Int32 i) 在 System.Data.SqlClient.SqlDataReader.get_Item(String name) 在 yd.Button1_Click(Object sender, EventArgs e) 位置 h:\订餐1\yd.aspx.cs:行号 49 --------------------编程问答--------------------
调试下这个方法 Button1_Click
看看参数和返回结果 --------------------编程问答-------------------- sr1 中没有数据,你去read报错 --------------------编程问答-------------------- 我知道 sr1 中没数据我是要建立判断,我想判断是否已有相同记录来处理下面操作。帮忙写一下判断代码 --------------------编程问答-------------------- int t = sxc.nonquery(sqltem);
->
int t = 0;
try { sxc.nonquery(sqltem); } catch { } --------------------编程问答-------------------- int t = sxc.nonquery(sqltem);
->
int t = 0;
try { t = sxc.nonquery(sqltem); } catch { } --------------------编程问答-------------------- 在哪来加?这个在 sr1查询中已经出错,我是想判断sr1中是否含有此数据来进行后面操作,麻烦帮我在上面代码改一下
补充:.NET技术 , ASP.NET