在asp.net c中运行sql子查询#

问题描述:

大家好我是在mycollege项目上工作,并试图在asp.dot net中创建一个用户登录应用程序。 我在我的数据库中有两个表,一个是客户全部细节,另一个是客户登录细节 我想要做什么来计算第一个查询中的行,并且如果login_id和密码匹配,则它执行另一个查询来检索并显示客户第一个名字,下面 的redirectedd页面上就是我所做的任何其他类型的方法也欢迎 这里是我的代码在asp.net c中运行sql子查询#

protected void Login1_Authenticate(object sender, AuthenticateEventArgs e) 
{ 
    SqlConnection connection = new SqlConnection(conn); 
    connection.Open(); 
    cmd = new SqlCommand("select COUNT(*) from customer_login where login_id = @a and [email protected]",connection); 
    cmd.Parameters.AddWithValue("@a", Login1.UserName); 
    cmd.Parameters.AddWithValue("@b", Login1.Password); 
    string user_name; 
    int i = Convert.ToInt32(cmd.ExecuteScalar().ToString()); 
    if (i == 1) 
    { 
     e.Authenticated = true; 
     cmd = new SqlCommand("select f_name from customer where id = (select cust_id from customer_login where login_id = @a)", connection);//This query successfully runs in mssms but it gives error in aspx 
     // cmd.Parameters.AddWithValue("@c",new SqlCommand ("select cust_id from customer_login where login_id = @a")); 
     //cmd.Parameters.AddWithValue("@a", Login1.UserName); 

     sdr = cmd.ExecuteReader(); 
     sdr.Read(); 
     user_name = sdr["f_name"].ToString(); 
     sdr.Close(); 
     if (Session["productID"] != null) 
     { 
      Session["user"] = user_name.ToString(); 
      Response.Redirect("~/Detail/cart.aspx"); 
     } 
     else 
     { 
      Response.Redirect("Default.aspx"); 
     } 
    } 
    else 
    { 
     e.Authenticated = false; 
    } 

} 

的问题是,它提供了以下错误关键字“附近有语法错误‘选择’ 。 必须声明标量变量“@a”。“ thanku

+1

CMD指的是一个新的命令为下一个查询。所以你需要再次添加参数@a。或者,您可以使用cmd.CommandText属性来设置新的查询,而不是创建新的SqlCommand。在这种方式下,旧的参数将被使用。 – Priyank 2014-09-20 06:38:36

因为你还没有分配值@a

cmd = new SqlCommand("select f_name from customer where id = (select cust_id from customer_login where login_id = @a)", connection); 
cmd.Parameters.AddWithValue("@a",Value); 

//cmd.Parameters.AddWithValue("@a", Login1.UserName); 

取消注释此行以

cmd.Parameters.AddWithValue("@a", Login1.UserName); 

然后再检查