Friday, August 02, 2013

Save Button Code in ASP.Net

First Insert these lines at the top of code bind file

using System.Data;
using System.IO;
using System.Configuration;
using System.Data.SqlClient;


Second step is to write this line inside your class

string _connStr = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;


Third step is to write this code in save button event


protected void btnsubmit_Click(object sender, EventArgs e)
    {
        try
        {
            String struserid, strpassword, strfirstname, strmiddlename, strlastname;
            String stremailid, strmobilenumber, strcollegename;
            struserid = txtuserid.Text;
            strpassword = txtpassword.Text;
            strfirstname = txtfirstname.Text;
            strmiddlename = txtmiddlename.Text;
            strlastname = txtlastname.Text;
            stremailid = txtemailid.Text;
            strmobilenumber = txtmobile.Text;
            strcollegename = txtcollegename.Text;
            SqlConnection con = new SqlConnection();
            con.ConnectionString = _connStr;
            String strinsert = "insert into profiledata(userid,firstname,middlename,lastname,emailid,mobile,collegename,password) values('" + struserid + "','" + strfirstname + "','" + strmiddlename + "','" + strlastname + "','" + stremailid + "','" + strmobilenumber + "','" + strcollegename + "','" + fileName + "','" + imageBytes + "','" + strpassword + "')";
            con.Open();

            SqlCommand cmd = new SqlCommand(strinsert, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception exp)
        {
            Label1.Text = "Error in inserting Data";
        }



    }

No comments: