Efficient way to Save or Insert TextBox Value into SQL Server DataBase Table In Asp.net Using C#.Net VB.Net

In every cases we need to Save or Insert data into Database. For beginner’s here i will describe how we can “Save or Insert TextBox Value into SQL Server DataBase Table using Asp.net C#.Net VB.Net”. There are a lot of ways to Save or Insert Data into Sql Server Database. But I will show you how we can do it easily and more efficiently.

First create a Database table like:

CREATE TABLE [dbo].[Product_Info](
	[ID] [bigint] IDENTITY(1,1) NOT NULL,
	[Name] [varchar](max) NOT NULL,
	[Description] [varchar](max) NOT NULL,
 CONSTRAINT [PK_Product_Info] PRIMARY KEY CLUSTERED 
(
	[ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Now modify the Web.config File Like:

<configuration>
  <connectionStrings>
    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient"/>
    <!--<add name="BONConnection" connectionString="Data Source=XXX.com;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />-->
  </connectionStrings>
...................
...................

Note: If you use windows authentication then use the first key. Otherwise use second key line. Update server name, Database name, User name, Password as per your settings.

Now design the HTML like:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<table>
    <tr><td><b>Product Name:</b></td><td><asp:TextBox ID="txtProductName" runat="server"></asp:TextBox></td></tr>
    <tr><td><b>Product Description:</b></td><td><asp:TextBox ID="txtProductDescription" runat="server"></asp:TextBox></td></tr>
    <tr><td colspan="2"><asp:Label ID="lblInfo" runat="server" Font-Bold="True"></asp:Label></td></tr>
    <tr><td><asp:Button ID="cmdSave" runat="server" Text="Save" onclick="cmdSave_Click" /></td><td></td></tr>
</table>
</asp:Content>

Now under Save Button Add Below Code:
C# Code:

    protected void cmdSave_Click(object sender, EventArgs e)
    {
        string insertCmd = "INSERT INTO Product_Info VALUES (@Name, @Description)";
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
        {
            conn.Open();
            SqlCommand myCommand = new SqlCommand(insertCmd, conn);
            // Create parameters for the SqlCommand object
            // initialize with input-form field values
            myCommand.Parameters.AddWithValue("@Name",txtProductName.Text);
            myCommand.Parameters.AddWithValue("@Description", txtProductDescription.Text);
            myCommand.ExecuteNonQuery();
            lblInfo.Text = "Data Inserted or Saved Successfully !!";
            lblInfo.ForeColor = System.Drawing.Color.Green;
        }
    }

Note: Don’t forget to add “System.Data.SqlClient” and “System.Configuration” namespaces.

VB.Net Code:

    Protected Sub cmdSave_Click(sender As Object, e As System.EventArgs) Handles cmdSave.Click
        Dim insertCmd As String = "INSERT INTO Product_Info VALUES (@Name, @Description)"
        Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
            conn.Open()
            Dim myCommand As New SqlCommand(insertCmd, conn)
            ' Create parameters for the SqlCommand object
            ' initialize with input-form field values
            myCommand.Parameters.AddWithValue("@Name", txtProductName.Text)
            myCommand.Parameters.AddWithValue("@Description", txtProductDescription.Text)
            myCommand.ExecuteNonQuery()
            lblInfo.Text = "Data Inserted or Saved Successfully !!"
            lblInfo.ForeColor = System.Drawing.Color.Green
        End Using
    End Sub

Note: Don’t forget to add “System.Data.SqlClient” and “System.Configuration” namespaces.

Output:
Save or Insert TextBox Value into SQL Server DataBase Table In Asp.Net Using Csharp VB.Ne

Check the DataBase:
Save or Insert TextBox Value into SQL Server DataBase Table In Asp.Net Using Csharp VB.Net_2

Hope now you can Add or Save or Insert Asp.Net TextBox data into Sql Server Database.

Download Code Example C#        Download Code Example VB.Net

Posted in .Net, Asp.net, C#, VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code
     
 

*