How to insert data into sql server using C# ( Csharp ) asp.net

In this Article am sharing some of few common code to insert data into SQL Server. in this example am gonna use below information.

Database Name : theadal_db

Server Name : THEADAL-PC // user your SQL Server Instance ex: localhostSQLExpress

Table Name : Student_Information

Dont forgot to update Your connection string in web.config

Please download the sample file to test the application

Download Sample Project - Download

C# code :

        string sql = "insert into Student_Information(Student_Name, Department, Year)values(@Student_Name, @Department,@Year)";
        SqlConnection connection = ConnectingManager.Getcollege1Connection();
        SqlCommand cmd = new SqlCommand(sql, connection);
        cmd.Parameters.AddWithValue("@Student_Name", StudentNameTextBox.Text);
        cmd.Parameters.AddWithValue("@Department", DepartmentTextBox.Text);
        cmd.Parameters.AddWithValue("@Year", YearTextBox.Text);
        cmd.ExecuteNonQuery();
        connection.Close();

Connecting Manager :

  public static SqlConnection Getcollege1Connection()
    {

        string ConnectionString = ConfigurationManager.ConnectionStrings["theadal_dbConnectionString1"].ConnectionString;
        SqlConnection Connection  = new SqlConnection(ConnectionString);
        Connection.Open();
        return Connection;
        
    }

 

Download Sample Project - Download


Anto Sujesh Jan 13, 2017, 2:44 pm

Answers

No Discussions Found

Article Discussions

Please login to your account to leave a comments