0
1.4kviews
Write an ASP.NET program to insert a new record in database using C#.

Mumbai University > Information Technology > Sem 4 > Web Programming

Marks: 10M

Year: Dec 2014, Dec 2015

1 Answer
0
10views

HTML:

<asp:GridView ID="GridView1" runat="server" Width="550px" AutoGenerateColumns="false"
    AlternatingRowStyle-BackColor="#C2D69B" HeaderStyle-BackColor="green" ShowFooter="true">
    <Columns>
        <asp:TemplateField HeaderText="Customer Name">
            <ItemTemplate>
                <%# Eval("CustomerName") %>
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCustomerName" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Company Name">
            <ItemTemplate>
                <%# Eval("CompanyName") %> 
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCompanyName" runat="server" />
            </FooterTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="City">
            <ItemTemplate>
                <%# Eval("City") >%
            </ItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtCity" runat="server"></asp:TextBox>
            </FooterTemplate>
        </asp:TemplateField>
            <asp:TemplateField>
            <ItemTemplate>
            </ItemTemplate>
            <FooterTemplate>
         <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" CommandName = "Footer" />
            </FooterTemplate>
        </asp:TemplateField>
    </Columns>
    <AlternatingRowStyle BackColor="#C2D69B" />
    <EmptyDataTemplate>
        <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="Add" OnClick="Add" CommandName = "EmptyDataTemplate"/>
            </td>
        </tr>
    </EmptyDataTemplate>
</asp:GridView>

C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {        this.BindData();    }
}
 private void BindData()
{
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        string strQuery = "SELECT * FROM Customers";
        SqlCommand cmd = new SqlCommand(strQuery);
        using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.Connection = con;
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
    }  }
protected void Add(object sender, EventArgs e)
{
    Control control = null;
    if (GridView1.FooterRow != null)
    {        control = GridView1.FooterRow;      }
    else
    {         control = GridView1.Controls[0].Controls[0];     }
    string customerName = (control.FindControl("txtCustomerName") as TextBox).Text;
    string companyName = (control.FindControl("txtCompanyName") as TextBox).Text;
    string city = (control.FindControl("txtCity") as TextBox).Text;
    string strConnString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
         using (SqlCommand cmd = new SqlCommand())
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "INSERT INTO [Customers] VALUES(@CustomerName, @CompanyName, @City)";
            cmd.Parameters.AddWithValue("@CustomerName", customerName);
            cmd.Parameters.AddWithValue("@CompanyName", companyName);
            cmd.Parameters.AddWithValue("@City", city);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }    }
    Response.Redirect(Request.Url.AbsoluteUri);
}
Please log in to add an answer.