Insert Records into
database 

Database
The below screenshot displays the
structure of the database table that will store the customer records

.ASPX Code:
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Inserting
.aspx.cs" Inherits="Inserting_" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD
XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
     <tr style="background-color:
Green;">
            <th scope="col">
               
Customer Name
            </th>
            <th scope="col">
               
Company Name
            </th>
            <th scope="col">
               
City
            </th>
            <th scope="col">
            </th>
        </tr>
        <tr>
            <td>
               
<asp:TextBox ID="txtCustomerName"
runat="server"
/>
            </td>
            <td>
               
<asp:TextBox ID="txtCompanyName"
runat="server"
/>
            </td>
            <td>
               
<asp:TextBox ID="txtCity"
runat="server"
/>
            </td>
            <td>
               
<asp:Button ID="btnAdd" runat="server"
Text="Insert
Method1" OnClick="Add" CommandName
= "EmptyDataTemplate"
/>
            </td>
        </tr>
        </table>
    </div>
    <asp:Label ID="Label1" runat="server"
Text="Label"></asp:Label>
    <asp:Button ID="Button1"
runat="server"
onclick="Button1_Click"
        Text="Insert
Method2" />
    <asp:Label ID="Label2" runat="server"
Text="Label"></asp:Label>
    </form>
</body>
</html>
.CS CODE:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
public partial class Inserting_ :
System.Web.UI.Page
{
    string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    protected void
Page_Load(object sender, EventArgs e)
    {
    }
    protected void Add(object sender, EventArgs
e)
    {
        SqlConnection con = new
SqlConnection(strConnString);
       
con.Open();
         SqlCommand  cmd = new SqlCommand("INSERT
INTO Customers VALUES('" + txtCustomerName.Text + "', '" + txtCompanyName.Text + "','" + txtCity.Text + "')", con);
       
cmd.ExecuteNonQuery();
       
con.Close();
       
clear();
       
Label1.Text = " Records Insrted
Successfully by First Method";
    }
    protected void
Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new
SqlConnection(strConnString);
       
con.Open();
  SqlCommand cmd = new
SqlCommand("INSERT
INTO [Customers] VALUES(@CustomerName, @CompanyName, @City)", con);
       
cmd.Parameters.AddWithValue("@CustomerName",
txtCustomerName.Text);
       
cmd.Parameters.AddWithValue("@CompanyName",
txtCompanyName.Text);
       
cmd.Parameters.AddWithValue("@City",
txtCity.Text);
       
cmd.ExecuteNonQuery();
       
con.Close();
       
clear();
       
Label2.Text = " Records Insrted
Successfully by SEcond Method";
    }
    void clear()
    {
       
txtCustomerName.Text = "";
       
txtCompanyName.Text = "";
       
txtCity.Text = "";
    }
}
 
 
No comments:
Post a Comment