从sql server获取asp.net中的记录时,它总是返回null [英] While fetching a record in asp.net from sql server it always return null

查看:52
本文介绍了从sql server获取asp.net中的记录时,它总是返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从sql server中获取asp.net中的记录时,它总是返回null ..其代码 - 即使数据库中有记录,对象oid在运行时也会为null。请帮忙。谢谢。

 受保护  void  btnLogin_Click(  object  sender,EventArgs e)
{
string cs = ConfigurationManager.ConnectionStrings [ DBMS]。ConnectionString;
SqlConnection con = new SqlConnection(cs);
SqlCommand com = new SqlCommand( select来自reg的id,其中user = @ user和password = @ pass,con);
com.Parameters.AddWithValue( @ user,txUName.Text);
com.Parameters.AddWithValue( @ pass,txPass.Text);
con.Open();
var oid = com.ExecuteScalar();
con.Close();

if (oid == null
{
lblEr.Visible = true ;
}
else
{
Session.Add( id,oid.ToString());
Session.Add( user,txUName.Text);

com = new SqlCommand( 从dbo.reg中选择类型,其中user = @ u,con);
com.Parameters.AddWithValue( @ u,txUName.Text);
con.Open();
var otype = com.ExecuteScalar();
con.Close();

var vtype = 管理员;
if (vtype == otype.ToString())
{
Session.Add( admin,otype.ToString());
Response.Redirect( admin.aspx);
}
Response.Redirect( user.aspx);

}
}

解决方案

它只是通过纠正以下语法来实现 -



SqlCommand com = new SqlCommand(select reg from reg where [user] = @ user and [password] = @ pass,con);



因为'user'也是sql server中用户的名字,所以当我们想在像这里作为列的其他地方使用它时,我们必须将它用作[user]。

While fetching a record in asp.net from sql server it always return null..here's code- Even if record is available in database, object oid will have null at run time. Please help. thanks.

protected void btnLogin_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBMS"].ConnectionString;
    SqlConnection con = new SqlConnection(cs);
    SqlCommand com = new SqlCommand("select id from reg where user=@user and password=@pass", con);
    com.Parameters.AddWithValue("@user", txUName.Text);
    com.Parameters.AddWithValue("@pass", txPass.Text);
    con.Open();
    var oid = com.ExecuteScalar();
    con.Close();

    if (oid == null)
    {
        lblEr.Visible = true;
    }
    else
    {
        Session.Add("id", oid.ToString());
        Session.Add("user", txUName.Text);

        com = new SqlCommand("select type from dbo.reg where user=@u", con);
        com.Parameters.AddWithValue("@u", txUName.Text);
        con.Open();
        var otype = com.ExecuteScalar();
        con.Close();

        var vtype = "admin";
        if (vtype == otype.ToString())
        {
            Session.Add("admin", otype.ToString());
            Response.Redirect("admin.aspx");
        }
        Response.Redirect("user.aspx");

    }
}

解决方案

It worked just by correcting following syntax-

SqlCommand com = new SqlCommand("select id from reg where [user]=@user and [password]=@pass", con);

Since 'user' is also name of a user in sql server, so when we want to use it at another place like here as column then we have to use it as [user].


这篇关于从sql server获取asp.net中的记录时,它总是返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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