i hv 3 different user,user selection is based on radiobuttonlist and then login button,but im having error that "use of unassigned local variable 'sql'.I couldnt figure out the mistake.do help me :)

protected void btn_Login_Click(object sender, EventArgs e)
    {
        string sql;

        if (btn_user.SelectedItem.Text == "Admin")
        {
           sql = "Select * from Admin where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        else if (btn_user.SelectedItem.Text == "Agent")
        {
            sql = "Select * from Agent where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        else if (btn_user.SelectedItem.Text == "Student")
        {
            sql = "Select * from Student where Username='" + txt_Luser.Text + "' and Password='" + txt_Lpass.Text + "'";
        }

        conn.open;

        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.CommandType = CommandType.Text;     

         SqlDataReader sdr = cmd.ExecuteReader();

         if (sdr.HasRows)
         {
             Session["User"] = txt_Luser.Text;
             Response.Redirect("Home.aspx");

         }
         else
         {
             txt_Luser.Text = string.Empty;
             lbl_msg.Text = "Please enter correct Username and Password";

         }       

         conn.Close();

    }

Recommended Answers

All 2 Replies

Initialise the string sql to some value and the warning will go away.
Even this is fine:
string sql = null;
The compiler simply wants to know that not initialising the variable was a deliberate action rather than a mistake.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.