fiyin ok

fiyin ok

  • NA
  • 7
  • 2.5k

inserting into gridview

Aug 26 2012 10:52 AM
Hi'
please am kind of new to programming and use of c# culd you help me with the follwing lines of code.
1. Am trying to insert into my database from a gridview footer and data is not entering into the db.
HERE IS THE 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.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class trial : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    public string conStr { get; set; }

    public void InsertNew(string AccountNumber, int BookBalance, int AvailableBalance, string AccountName)
    { 

        SqlConnection con = new SqlConnection(conStr); 
        SqlCommand cmd = new SqlCommand("InsertCustomer", con); 
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@accountNumber", AccountNumber);

        cmd.Parameters.AddWithValue("@bookBalance", BookBalance);

        cmd.Parameters.AddWithValue("@availableBalance", AvailableBalance);

        cmd.Parameters.AddWithValue("@accountName", AccountName);

        using (con)
        {

            con.Open();
            cmd.ExecuteNonQuery();

        }

    }

   

    protected void btnAdd_Click(object sender, EventArgs e)
    {
       

    }
}






<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="trial.aspx.cs" Inherits="trial" %>

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
   
<asp:GridView ID="GridView1" runat="server" ShowFooter = "True"
        AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
      <Columns>
                <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False"
                    ReadOnly="True" SortExpression="id" />
              
                <asp:TemplateField HeaderText="Account Number" SortExpression="accountNumber">
                <ItemTemplate>
                <asp:Label ID= "lblAcctNum" runat="server" Text ='<%# Bind ("accountNumber") %>'>
                </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:TextBox ID = "txtAcctNum" runat ="server" Text = '<%# Bind ("accountNumber") %>'>
                </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID ="txtAcctNum" runat ="server"/>
                </FooterTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="Book Balance" SortExpression="bookBalance">
                <ItemTemplate>
                <asp:Label ID = "lblBookBal" runat ="server" Text ='<%# Bind("bookBalance") %>'>
                </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:TextBox ID ="txtBookBal" runat ="server" Text ='<%# Bind ("bookBalance") %>'>
                </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID ="txtBookBal" runat="server" />
                </FooterTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="Avaliable Balance" SortExpression="availableBalance">
                <ItemTemplate>
                <asp:Label ID = "lblAvailBal" runat ="server" Text = '<%# Bind ("availableBalance") %>'>
                </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:TextBox ID ="txtAvailBal" runat ="server" Text ='<%# Bind ("availableBalance") %>'>
                </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID="txtAvailBal" runat ="server" />
                </FooterTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="Account Name" SortExpression="accountName">
                <ItemTemplate>
                <asp:Label ID = "lblAcctNam" runat ="server" Text ='<%# Bind("accountName") %>'>
                </asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                <asp:TextBox ID ="txtAcctNam" runat="server" Text='<%# Bind("accountName") %>'>
                </asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                <asp:TextBox ID="txtAcctNam" runat ="server" />
                </FooterTemplate>
                </asp:TemplateField>
               
                <asp:TemplateField HeaderText="I">
                <FooterTemplate>
                <asp:Button ID ="btnAdd" CommandName = "Insert" Text="Add" runat="server"
                        onclick="btnAdd_Click" />
                </FooterTemplate>
                </asp:TemplateField>
               
                <asp:CommandField ButtonType="Button" ShowCancelButton="False"
                    ShowDeleteButton="True" ShowEditButton="True" />
            </Columns>
    </asp:GridView>
   

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
       
ConnectionString="<%$ ConnectionStrings:AccountDBConnectionString %>"
       
DeleteCommand="DELETE FROM [GLAccounts] WHERE [id] = @id"
       
InsertCommand="INSERT INTO [GLAccounts] ([accountNumber], [bookBalance], [availableBalance], [accountName]) VALUES (@accountNumber,

@bookBalance, @availableBalance, @accountName)"
       
SelectCommand="SELECT [id], [accountNumber], [bookBalance], [availableBalance], [accountName] FROM [GLAccounts]"
       
UpdateCommand="UPDATE [GLAccounts] SET [accountNumber] = @accountNumber, [bookBalance] = @bookBalance, [availableBalance] =

@availableBalance, [accountName] = @accountName WHERE [id] = @id">
       
<DeleteParameters>
           
<asp:Parameter Name="id" Type="Int64" />
       
</DeleteParameters>
      
 <InsertParameters>
           
<asp:ControlParameter ControlID="GridView1" Name="accountNumber"
              
 PropertyName="SelectedDataKey" Type="String" />
           
<asp:ControlParameter ControlID="GridView1" Name="bookBalance"
               
PropertyName="SelectedDataKey" Type="Decimal" />
           
<asp:ControlParameter ControlID="GridView1" Name="availableBalance"
               
PropertyName="SelectedDataKey" Type="Decimal" />
           
<asp:ControlParameter ControlID="GridView1" Name="accountName"
              
 PropertyName="SelectedDataKey" Type="String" />
       
</InsertParameters>
       
<UpdateParameters>
           
<asp:Parameter Name="accountNumber" Type="String" />
           
<asp:Parameter Name="bookBalance" Type="Decimal" />
           
<asp:Parameter Name="availableBalance" Type="Decimal" />
           
<asp:Parameter Name="accountName" Type="String" />
           
<asp:Parameter Name="id" Type="Int64" />
       
</UpdateParameters>
   
</asp:SqlDataSource>
   
</asp:Content>



Answers (2)