.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ADO.NET >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Inserting values from textbox in the database tables.

Posted By:Ashutosh Jha       Posted Date: May 01, 2014    Points: 40    Category: ADO.NET    URL: http://www.dotnetspark.com  

Here i will explain how to insert the textbox values in database tables. Here i have established connection in web.config
 

IN Web.config, create the connection like this-
    <connectionStrings>
        <add name="conn2" connectionString="Server=ASHUTOSH-PC\SQLEXPRESS;Database=topline;Trusted_Connection=Yes;"/>
    </connectionStrings>

and on submit button click, code will be like below- 

protected void submitbtn_Click(object sender, EventArgs e)
    {
        SqlConnection conn2 = null;
        try
        {
            DateTime dt = DateTime.Now;
            string connStr = ConfigurationManager.ConnectionStrings["conn2"].ToString();
            conn2 = new SqlConnection(connStr);
            conn2.Open();
            string sql = "INSERT INTO tblcareer(PNAME,REQUIRED1,EXPERIENCE,LOCATION,POSTINGDATE, REQBYDATE, JOBDESCRIPTION) VALUES (@PNAME,@REQUIRED1,@EXPERIENCE,@LOCATION,@POSTINGDATE, @REQBYDATE, @JOBDESCRIPTION)";
            SqlCommand cmd = new SqlCommand(sql, conn2);
            cmd.Parameters.AddWithValue("@PNAME", name.Text);
           // cmd.Parameters.AddWithValue("@QUALIFICATION", qualification.Text);
            cmd.Parameters.AddWithValue("@REQUIRED1", rbgender.SelectedItem.Text);
            cmd.Parameters.AddWithValue("@EXPERIENCE", exp.SelectedItem.Text);
            //cmd.Parameters.AddWithValue("@VACROLE", reqrole.Text);
            cmd.Parameters.AddWithValue("@LOCATION", location.Text);
            cmd.Parameters.AddWithValue("@POSTINGDATE", jobdate.Text);
            cmd.Parameters.AddWithValue("@REQBYDATE", reqdate.Text);
            cmd.Parameters.AddWithValue("@JOBDESCRIPTION", desc.Text);
            cmd.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            if (conn2.State == ConnectionState.Open)
            {
                conn2.Close();
            }
        }
        ClientScript.RegisterClientScriptBlock(this.GetType(), "myscript", "alert('Vacancy Created Successfully');", true);
        name.Text = "";
       // qualification.Text = "";
        exp.Text = "";
        reqdate.Text = "";
       // reqrole.Text = "";
        location.Text = "";
        desc.Text = "";
        // jobdate.Text = "";
    }


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend