没有数据时如何解决它的无效尝试 [英] Invalid attempt to read when no data is present how to solve it

查看:131
本文介绍了没有数据时如何解决它的无效尝试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

i写c#代码来检查用户类型= 1是否依赖于值来自datareader

但我得到了这个错误当没有数据时无效的读取尝试礼物



这是我的代码

hello
i write c# code to check if the user type = 1 depended on value comes from datareader
but i got this error Invalid attempt to read when no data is present

this is my code

protected void Page_Load(object sender, EventArgs e)
    {
        string name = "";
        if (Session["usernames"] != "" & Session["usernames"] != null)
        {
            name = Session["usernames"].ToString();
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
            SqlCommand command = new SqlCommand("SELECT user_type FROM dbo.user_data where user_name='" + name + "'");
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.HasRows)
            {
                if (reader["user_type"] == "1")
                {
                    Response.Redirect("../admincp/Default.aspx");
                }
                else
                {
                    Response.Redirect("../Access_Denied.aspx");
                }
            }
           
                
        }
        
       
    }





所以你可以帮我解决这个错误:(先谢谢了)


#Improving the问题



so can you plz help me with this error :( thanks in advanced


#Improving the question

string name = "";
if (Session["usernames"] != "" & Session["usernames"] != null)
{
name = Session["usernames"].ToString();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT user_type FROM user_data where user_name='"+name+"'");
command.Connection = connection;
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{

reader.Read();

if (reader["user_type"]== "1")
{
Response.Redirect("../admincp/Default.aspx");

}
else
{
Response.Redirect("../Access_Denied.aspx");

}
}





当我使用此代码并且我确定user_type值是1它将我重定向到access_denied.aspx idk为什么

当我将代码更改为



when i use this code and iam sure that the user_type value is 1 it's redirect me to access_denied.aspx idk Why
and when i change the code to

string name = "";
if (Session["usernames"] != "" & Session["usernames"] != null)
{
name = Session["usernames"].ToString();
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
SqlCommand command = new SqlCommand("SELECT user_type FROM user_data where user_name='"+name+"'");
command.Connection = connection;
command.CommandType = CommandType.Text;
connection.Open();
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
if (reader.HasRows)
{

reader.Read();
string type = reader["user_type"].ToString();
if (type == "1")
{
Response.Redirect("../admincp/Default.aspx");
//Response.Redirect("../Access_Denied.aspx");
}
else
{
Response.Redirect("../Access_Denied.aspx");
//Response.Redirect("../admincp/Default.aspx");
}
}





i进入无限循环:(我恨我的自我无法集中
等待最佳答案

提前感谢



i enter infinite loop :( i hate my self i can't focus
waiting for the best answer
thanks in advance

推荐答案

if(reader.HasRows)

$ / b
$

if(读者

[user_type] = =1)
if (reader.HasRows)

{
reader.read();
if (reader
[ "user_type" ] == "1" )


如果你有超过1个结果,你可以做



if you have more than 1 result, you can do

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
  // read data
}


这里的答案

http://www.c-sharpcorner.com/Forums/Thread/266404/无效 - 尝试阅读 - 无数据 - 现在.aspx [ ^ ]


这篇关于没有数据时如何解决它的无效尝试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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