connection to database

//put into web.config
<connectionStrings>
        <add name="connectDb" connectionString="Data Source=DEEPAK;Initial Catalog=software;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>


//Call method



protected void btnSubmit_Click(object sender, EventArgs e)
    {
      
        string userLoginId = Convert.ToString(txtLoginId.Text);
        string Fname =Convert.ToString(txtfname.Text);
        string Lname = Convert.ToString(txtlname.Text);
        string Pass = Convert.ToString(txtpassword.Text);
        System.Web.UI.WebControls.FileUpload img = (System.Web.UI.WebControls.FileUpload)FileUpload1 ;
        Byte[] imgByte = null;
        if (img.HasFile && img.PostedFile != null)
        {
           
            HttpPostedFile File = FileUpload1.PostedFile;
           
            imgByte = new Byte[File.ContentLength];
           
            File.InputStream.Read(imgByte, 0, File.ContentLength);
        }

        int k=check();
        if (k == 0)
        {

            int id = Convert.ToInt32(genobj.regestration(userLoginId, Fname, Lname, Pass, imgByte));
            Response.Write(id);
            Response.Write("registered succesfully");
            Response.Redirect("dasbord.aspx");
           
        }
        else
        {
            Response.Write("Try again");

        }
      
    }





// call to database connection


 public DataTable login(String LogNm, String Pass)
    {
        SqlParameter[] param = new SqlParameter[2];

        param[0] = new SqlParameter("@LoginId", SqlDbType.NVarChar, 20);
        param[0].Direction = ParameterDirection.Input;
        param[0].Value = LogNm;

        param[1] = new SqlParameter("@Password", SqlDbType.NVarChar,100);
        param[1].Direction = ParameterDirection.Input;
        param[1].Value = Pass;
        DataTable dt = dbobj.checkLogin("Sp_LogIn", param);
        return dt;
    }

    public int checkLoginId(string loginId)
    {
        SqlParameter[] param = new SqlParameter[2];
        param[0] = new SqlParameter("@LoginId", SqlDbType.NVarChar, 20);
        param[0].Direction = ParameterDirection.Input;
        param[0].Value = loginId;

        param[1] = new SqlParameter("@ID", SqlDbType.Int);
        param[1].Direction = ParameterDirection.Output;

        DataTable dt = dbobj.checkLogin("SP_check_LoginId", param);
        int i=0;
        if (dt.Rows.Count > 0)
        {
            return i = 1;
        }
        else
        {
            return i;
        }
    }
    public int regestration(string LoginId, String fname, String lname, String password, Byte[] img)
    {
        SqlParameter[] param = new SqlParameter[6];

        param[0] = new SqlParameter("@FName", SqlDbType.NVarChar, 20);
        param[0].Direction = ParameterDirection.Input;
        param[0].Value = fname;

        param[1] = new SqlParameter("@LName", SqlDbType.NVarChar, 20);
        param[1].Direction = ParameterDirection.Input;
        param[1].Value = lname;

        param[2] = new SqlParameter("@Password", SqlDbType.NVarChar, 100);
        param[2].Direction = ParameterDirection.Input;
        param[2].Value = password;

        param[3] = new SqlParameter("@Image",SqlDbType.Image);
        param[3].Direction = ParameterDirection.Input;
        param[3].Value = img;

        param[4]=new SqlParameter("@LastId",SqlDbType.Int);
        param[4].Direction=ParameterDirection.Output;

        param[5] = new SqlParameter("@LoginId", SqlDbType.NVarChar);
        param[5].Direction = ParameterDirection.Input;
        param[5].Value = LoginId;

        int k=dbobj.DbInsert("Sp_registration",param);
        int id=0;
        if(k>0)
        {
         id=Convert.ToInt32(param[4].Value);

        }

        return id;
    }


// now create connection


 public void connectDb()
    {
        con = new SqlConnection(Convert.ToString(ConfigurationManager.AppSettings["connectDb"]));
        if (con.State == ConnectionState.Closed || con.State == ConnectionState.Broken)
        {
            con.Open();

        }
  
    }

//now call for coonection

public int DbInsert(string Spname, SqlParameter[] param)
    {
        connectDb();
        cmd = new SqlCommand(Spname, con);
        cmd.CommandType = CommandType.StoredProcedure;
        for (int i = 0; i < param.Length; i++)
        {
            cmd.Parameters.Add(param[i]);
        }
        int j = cmd.ExecuteNonQuery();
        con.Close();
        return j;
    }
    public DataTable checkLogin(string sp_Check, SqlParameter[] param)
    {
        connectDb();

        //cmd = new SqlCommand(sp_Check, con);
        //cmd.CommandType = CommandType.StoredProcedure;
        //for (int i = 0; i < param.Length; i++)
        //{
        //    cmd.Parameters.Add(param[i]);
        //}
        //int j = cmd.ExecuteNonQuery();
        //con.Close();
        //return j;
        da = new SqlDataAdapter(sp_Check,con);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
       
        for (int i = 0; i < param.Length; i++)
        {
            da.SelectCommand.Parameters.Add(param[i]);
        }
        DataTable dt = new DataTable();
        da.Fill(dt);

        return dt;
    }

No comments:

Post a Comment