与Ado.net使用会话时出错 [英] error in using sessions with Ado.net
本文介绍了与Ado.net使用会话时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
cmd.CommandText = SP_user_verifi;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ Name,nameforlogin.Text);
cmd.Parameters.AddWithValue( @ Password,pwdforlogin.Text);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
if (ds!= null && ds.Tables。计数> 0 )
{
string status = ds.Tables [ 0 ]。行[ 0 ] [< span class =code-digit> 0 ]。ToString();
if (status == yes )
会话[ Id ] = ds.Tables [ 1 ]。行[ 0 ] [ id]。ToString();
会话[ nameforlogin] = ds.Tables [ 1 ]。行[ 0 ] [ 名称跨度>]的ToString();
会话[ pwdforlogin] = ds.Tables [ 1 ]。行[ 0 ] [ 密码]。ToString();
response.redirect(test.aspx);
和我的存储过程是
ALTER proc [ dbo]。[SP_user_verifi]
(
@ Name nvarchar (MAX),
@ Password nvarchar (MAX)
)
as
begin
if 存在(选择 * 来自 tb_userlogin 其中名称= @名称和密码= @密码)
开始
选择 ' yes' as status
end
else
开始
选择 ' no' as status
end
end
在上面的代码我有错误,就像在这一行找不到表1 < br $>
会话[Id] = ds.Tables [1] .Rows [0] [id]。ToString() ;
任何人都可以帮助我..
谢谢和问候
解决方案
我认为这个问题不是由会话变量引起的。
检查这行代码,我想如果你改变了它,它会运行良好:
// 会话[Id] = ds.Tables [ 1] .Rows [0] [id]。ToString();
会话[ Id] = ds.Tables [ 0 ]。行[ 0 ] [ id]。ToString();
HI,
从存储过程中,只有一个返回的select语句。所以,它返回表计数1.
试试这个。
cmd.CommandText =SP_user_verifi;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ Name,nameforlogin.Text);
cmd.Parameters.AddWithValue(@ Password,pwdforlogin.Text);
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
da.SelectCommand = cmd;
da.Fill(dt);
if(dt!= null&& dt.Rows.Count> 0)
{
Session [Id] = dt.Rows [0] [id]的ToString();
//剩余代码
}
或
cmd.CommandText =SP_user_verifi ;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue(@ Name,nameforlogin.Text);
cmd.Parameters.AddWithValue(@ Password,pwdforlogin.Text);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataTable();
da.SelectCommand = cmd;
da.Fill(ds);
if(ds!= null&& ds.Tables.Count> 0)
{
Session [Id] = ds.Tables [0] .Rows [0] [ ID]的ToString(); //因为它只返回1个表
//剩余代码
}
希望它可以帮到你。
谢谢
cmd.CommandText = "SP_user_verifi";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", nameforlogin.Text);
cmd.Parameters.AddWithValue("@Password", pwdforlogin.Text);
SqlDataAdapter da = new SqlDataAdapter();
DataSet ds = new DataSet();
da.SelectCommand = cmd;
da.Fill(ds);
if (ds != null && ds.Tables.Count > 0)
{
string status = ds.Tables[0].Rows[0][0].ToString();
if (status == "yes")
Session["Id"] = ds.Tables[1].Rows[0]["id"].ToString();
Session["nameforlogin"] = ds.Tables[1].Rows[0]["Name"].ToString();
Session["pwdforlogin"] = ds.Tables[1].Rows[0]["Password"].ToString();
response.redirect("test.aspx");
and my stored proc is
ALTER proc [dbo].[SP_user_verifi]
(
@Name nvarchar(MAX),
@Password nvarchar(MAX)
)
as
begin
if exists (select * from tb_userlogin where Name=@Name and Password=@Password )
begin
select 'yes' as status
end
else
begin
select 'no' as status
end
end
in the above code i have error like cannot find table 1 at this line
Session["Id"] = ds.Tables[1].Rows[0]["id"].ToString();
can any one help me..
Thanks and Regards
解决方案
I think the problem wasn't caused by the session variable.
Check this line of code, I think if you change this it will work well:
//Session["Id"] = ds.Tables[1].Rows[0]["id"].ToString(); Session["Id"] = ds.Tables[0].Rows[0]["id"].ToString();
HI,
From stored procedure you have only one select statement which is returning. So, It returns Table count 1.
Try this.
cmd.CommandText = "SP_user_verifi"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", nameforlogin.Text); cmd.Parameters.AddWithValue("@Password", pwdforlogin.Text); SqlDataAdapter da = new SqlDataAdapter(); DataTable dt = new DataTable(); da.SelectCommand = cmd; da.Fill(dt); if (dt!= null && dt.Rows.Count > 0) { Session["Id"] = dt.Rows[0]["id"].ToString(); //remaining code }
or
cmd.CommandText = "SP_user_verifi"; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@Name", nameforlogin.Text); cmd.Parameters.AddWithValue("@Password", pwdforlogin.Text); SqlDataAdapter da = new SqlDataAdapter(); DataSet ds = new DataTable(); da.SelectCommand = cmd; da.Fill(ds); if (ds!= null && ds.Tables.Count > 0) { Session["Id"] = ds.Tables[0].Rows[0]["id"].ToString(); // since it returns only 1 table //remaining code }
Hope it helps you.
Thanks.
这篇关于与Ado.net使用会话时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文