C# : Tutorial of Asp.net with c# and example

Example and code about windows application, web application, SQL, C#, WPF etc which is helpful to developer and programer.

Search This Blog

How to insert data in sql server using asp with c sharp

♠ Posted by uvesh in at Monday, February 22, 2016

Insert Data in Sql server 2012


  protected void InsertBtn_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(@"Data Source=Home\SQLExpress;Initial Catalog=MyDatabase;Integrated Security=True");
        SqlCommand insert = new SqlCommand("insert into tblUser(Uid, Name, Mobile, Email) values(@uid, @name, @mobile, @Email)", conn);
        //SqlCommand insert = new SqlCommand("insert into tblUser values('"+1+"','"+Name.Text+"','"+Mobile.Text+"','"+Email.Text+"')",conn);
        insert.Parameters.AddWithValue("@uid", 1);
        insert.Parameters.AddWithValue("@name", Name.Text);
        insert.Parameters.AddWithValue("@mobile", Mobile.Text);
        insert.Parameters.AddWithValue("@Email", Email.Text);
        try
        {
            conn.Open();
            insert.ExecuteNonQuery();

        }
        catch (Exception ex)
        {
            LMsg.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
        Name.Text = "";
        Mobile.Text = "";
        Email.Text = "";

    }

Improve Your Answer as comment below.

If our post is useful please share and comment on our post for more post and detail Visit :


http://aspdotnethelpmaster.blogspot.in/

1 comments:

The blog gave me idea to insert data in to sqlserver my sincere thanks for sharing this post
Dot Net Training in Chennai

Post a Comment