2015-11-13 20 views
0

我试图根据谁登录谁获取Team_ID并将其分配给会话变量来运行SQL查询。我无法将结果分配给变量。将SQL查询结果分配给会话变量

protected void ButtonLogin_Click(object sender, EventArgs e) 
{ 
    //check what user category was selected and login to appropriate page 
    if (DropDownListUserType.SelectedIndex == 1) 
    { 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Web_FussConnectionString"].ConnectionString); 

     con.Open(); 
     SqlCommand cmd = new SqlCommand("select * from Team_User where Email = @username and Password_1 = @password", con); 

     cmd.Parameters.AddWithValue("@username", UserName.Text); 
     cmd.Parameters.AddWithValue("@password", Password.Text); 

     SqlCommand cmdID = new SqlCommand("select Team_ID from Team_User where Email = @username and Password_1 = @password", con); 

     cmdID.Parameters.AddWithValue("@username", UserName.Text); 
     cmdID.Parameters.AddWithValue("@password", Password.Text); 

     SqlDataAdapter da = new SqlDataAdapter(cmd); 
     DataTable dt = new DataTable(); 
     da.Fill(dt); 

     if (dt.Rows.Count > 0) 
     { 
      SqlDataReader reader = cmdID.ExecuteReader(); 
      int Team_ID = reader.GetInt32(1); 
      Session["Team_ID"] = Team_ID; 
      Response.Redirect("AddPlayer.aspx"); 
     } 
     else 
     { 
      ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>"); 
     } 
    } 
} 
+2

有麻烦 - 有什么麻烦吗? – MusicLovingIndianGirl

+0

你的错误是什么? – Diginari

+0

为什么在第一个查询已经返回所有您感兴趣的数据时,您会执行* second *查询?只需获取数据表中的行,并检查“Team_ID”列......绝对不需要第二次数据库调用..... –

回答

0

您的代码不作一大堆的感觉....

如果你只Team_ID - 你为什么加载整排第一,然后调用数据库再次得到只是Team_ID ???

我试图简化代码定好位:

protected void ButtonLogin_Click(object sender, EventArgs e) 
{ 
    // check what user category was selected and login to appropriate page 
    if (DropDownListUserType.SelectedIndex == 1) 
    { 
     // define connection string and SQL query as strings 
     string connectionString = ConfigurationManager.ConnectionStrings["Web_FussConnectionString"].ConnectionString; 
     string query = "SELECT Team_ID FROM dbo.Team_User WHERE Email = @username AND Password_1 = @password"; 

     // set up SqlConnection and SqlCommand in "using" blocks 
     using (SqlConnection con = new SqlConnection(connectionString)) 
     using (SqlCommand cmd = new SqlCommand(query, con)) 
     { 
      // define and fill parameters - DO NOT use .AddWithValue! 
      cmd.Parameters.Add("@username", SqlDbType.VarChar, 100).Value = UserName.Text; 
      cmd.Parameters.Add("@password", SqlDbType.VarChar, 100).Value = Password.Text; 

      // open connection, execute scalar, close connection 
      con.Open(); 

      object result = cmd.ExecuteScalar(); 

      // if we got back a result .... 
      if(result != null) 
      { 
       int teamID = Convert.ToInt32(result.ToString()); 

       Session["Team_ID"] = teamID; 
       Response.Redirect("AddPlayer.aspx"); 
      } 
      else 
      { 
       // if result is NULL, then the username+password 
       // were NOT found - do what needs to be done in that case here 
      } 
     } 
    } 
} 
+0

我也需要它来检查用户名和密码是否存在登录到系统。这就是为什么查询有 – Haldamir

+2

@Haldamir:如果用户名+密码不存在,您将从该单个查询中返回“NULL”,并且您也知道 - 不需要第二个查询 –

相关问题