ASP.NET中“在没有任何数据时进行无效的读取尝试。
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSys...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
public partial class Person : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserName"].ToString() != "")
{
if (!Page.IsPostBack)
{
ShowUserInfo();
}
}
else
{
Response.Redirect("Login.aspx");
}
}
public void ShowUserInfo()
{
string strConn = System.Configuration.ConfigurationManager.AppSettings["DSN"];
SqlConnection conn = new SqlConnection(strConn);
lblName.Text = Session["UserName"].ToString();
string sql = "select RealName,Sex,Email from Users where UserName='" + lblName + "'";
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
reader.Close();
conn.Close();
}
}
这个代码运行提示“在没有任何数据时进行无效的读取尝试。”
如果改成后
if( reader.Read())
{
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
}
不会报错,但是读不出数据,显示为空!
欢迎大神指导! 展开
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Data.SqlTypes;
public partial class Person : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserName"] != null && Session["UserName"].ToString() != "")
{
if (!Page.IsPostBack)
{
ShowUserInfo();
}
}
else
{
Response.Redirect("Login.aspx");
}
}
public void ShowUserInfo()
{
string strConn = System.Configuration.ConfigurationManager.AppSettings["DSN"];
SqlConnection conn = new SqlConnection(strConn);
lblName.Text = Session["UserName"].ToString();
string sql = "select RealName,Sex,Email from Users where UserName='" + lblName + "'";
SqlCommand cmd = new SqlCommand(sql,conn);
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
reader.Close();
conn.Close();
}
}
这个代码运行提示“在没有任何数据时进行无效的读取尝试。”
如果改成后
if( reader.Read())
{
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
}
不会报错,但是读不出数据,显示为空!
欢迎大神指导! 展开
2个回答
展开全部
SqlDataReader 从数据流中一次去一条数据最起码要这样写
while (reader.Read())
{
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
}
reader.Close();
while (reader.Read())
{
lblRealName.Text = reader["RealName"].ToString();
lblSex.Text = reader["Sex"].ToString();
lblEmail.Text = reader["Email"].ToString();
}
reader.Close();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询