Friday, 10 March 2017

SQL Database connection with Store Procedure in c#.






using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class login : System.Web.UI.Page
{
    SqlConnection _con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\gtu.mdf;Integrated Security=True;User Instance=True");
    string uname, password;
    protected void Page_Load(object sender, EventArgs e)
    {
        uname = txt_username.text;
        password = txt_password.text;

        _con.Open();
        SqlCommand _cmd = _con.CreateCommand();
        _cmd.CommandType = CommandType.StoredProcedure;
        _cmd.CommandText = "c_login";
        _cmd.Parameters.Add("@unm", SqlDbType.VarChar).Value = uname.ToString();
        _cmd.Parameters.Add("@pwd", SqlDbType.VarChar).Value = password.ToString();

        _cmd.ExecuteNonQuery();
        DataTable _dt = new DataTable();
        SqlDataAdapter _da = new SqlDataAdapter(_cmd);
        _da.Fill(_dt);

        foreach (DataRow _dr in _dt.Rows)
        {
            Session["uname"] = _dr["unm"].ToString();
            Response.Write("aa");
        }
        _con.Close();
    }
}


//Store Procedure Create in DB

CREATE PROCEDURE c_tlogin
/*
Parameter Declare
*/
@unm varchar(50),
@pwd varchar(50)
AS
/* SET NOCOUNT ON */
select * from login where username=@unm and password=@pwd
RETURN

No comments:

Post a Comment