Monday 9 July 2012

For Login Using Stored Procedures instead of specific SQL statements is very useful in ASP.NET using c#




 protected void Button1_Click(object sender, EventArgs e)
    {
 //Procure1
           //   SPVerifyLogin(TextBox1.Text, TextBox2.Text);  


//Procedure 2
              // SPVerifyLogins(TextBox1.Text, TextBox2.Text);  

//Procedure 3
        SQLAdopterSPVerifyLogins(TextBox1.Text, TextBox2.Text); 

    }



Procedure 1:
private void SPVerifyLogin(string username, string password)
       {
           try
           {
               SqlConnection conn = (SqlConnection)NConnection.Connection.Connection_Method();
               SqlCommand cmd = new SqlCommand("ChechAdminLogin", conn);
               cmd.CommandType = CommandType.StoredProcedure;

              // cmd.Parameters.Add("@Adminlogin_Name", username);
               //cmd.Parameters.Add("@AdminLogin_Password", password);

               cmd.Parameters.AddWithValue("@AdminLogin_Name", username);
               cmd.Parameters.AddWithValue("@AdminLogin_Password", password);
               
               cmd.ExecuteNonQuery();

            //   lbl.Text = TextBox1.Text;
                            
cmd.Connection.Close();  //close Connection.
             
  ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('Password & UserName Correct'); window.location.href = 'HomeAdmin.aspx';", true);  
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
}



Procedure 2:


private void SPVerifyLogins(string username, string password)
    {
        try
        {
            ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SqlConnection conn = (SqlConnection)NConnection.Connection.Connection_Method();
            SqlCommand cmd = new SqlCommand("ChechAdminLogin", conn);
            cmd.CommandType = CommandType.StoredProcedure;

            // cmd.Parameters.Add("@Adminlogin_Name", username);
            //cmd.Parameters.Add("@AdminLogin_Password", password);

            cmd.Parameters.AddWithValue("@AdminLogin_Name", username);
            cmd.Parameters.AddWithValue("@AdminLogin_Password", password);

            da.SelectCommand = cmd;
            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
          
                ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert(' Password & UserName Correct '); window.location.href = 'HomeAdmin.aspx';", true);
               // Response.Redirect("HomeAdmin.aspx");  // throw a Exception
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", @"alert('Invalid Login Email & Password.');", true);
            }
         
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
       }


Procedure 3:


 private void SQLAdopterSPVerifyLogins(string username, string password)
    {
        try
        {
            ds = new DataSet();
           
// Create a Connection.
            SqlConnection conn = (SqlConnection)NConnection.Connection.Connection_Method();


//Create a DataAdapter, and then provide the name of the stored procedure.

            SqlDataAdapter da = new SqlDataAdapter("ChechAdminLogin", conn);


//Set the command type as StoredProcedure.

            da.SelectCommand.CommandType = CommandType.StoredProcedure;


// Add a parameter and Value to Parameters collection for the stored procedure.

            da.SelectCommand.Parameters.Add("@Adminlogin_Name", username);
            da.SelectCommand.Parameters.Add("@AdminLogin_Password", password);


//Fill the DataSet with the rows that are returned.

            da.Fill(ds);

            if (ds.Tables[0].Rows.Count > 0)
            {
              
                ClientScript.RegisterStartupScript(typeof(Page), "MessagePopUp", "alert('valid Login Email & Password.'); window.location.href = 'HomeAdmin.aspx';", true);
              
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", @"alert('Invalid Login Email & Password.');", true);
            }



        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }



No comments:

Post a Comment