Click here to Skip to main content
15,882,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I add a text box in my grid view & its working fine. But when i insert all grid view data into database the all grid view data are inserted in database except text box values. Now please help me how i will insert all data (including Text box values) into my database.
My Code is::
XML
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Datacollection.aspx.cs" Inherits="New_NPS.Datacollection" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <div>
     <script type="text/javascript">
      function isNumberKey(evt) {
            var charCode = (evt.which) ? evt.which : event.keyCode
            if (charCode > 31 && (charCode < 48 || charCode > 57))
                return false;
            return true;
        }

   </script>




    <h1> put collection amount</h1>
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
            AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Member_ID"
            DataSourceID="SqlDataSource1" CellPadding="4" ForeColor="#333333"
            GridLines="None" style="margin-right: 54px" Width="303px"
            onselectedindexchanged="GridView1_SelectedIndexChanged">

                <AlternatingRowStyle BackColor="White" />

                <Columns>
                <asp:BoundField DataField="Member_ID" HeaderText="Member_ID" ReadOnly="True"
                        SortExpression="Member_ID" />
                    <asp:BoundField DataField="First_Name" HeaderText="First_Name"
                        SortExpression="First_Name" ReadOnly="True" />
                    <asp:BoundField DataField="Last_Name" HeaderText="Last_Name"
                        SortExpression="Last_Name" ReadOnly="True" />


                    <asp:templatefield headertext="Collection Amount">
                    <itemtemplate>
                    <asp:textbox id="TextBox1" onkeypress="return isNumberKey(event)" type="Text" ReadOnly="False"  runat="server"></asp:textbox>
                    </itemtemplate>
                    </asp:templatefield>










                        </Columns>
                <EditRowStyle BackColor="#2461BF" />
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <RowStyle BackColor="#EFF3FB" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <SortedAscendingCellStyle BackColor="#F5F7FB" />
                <SortedAscendingHeaderStyle BackColor="#6D95E1" />
                <SortedDescendingCellStyle BackColor="#E9EBEF" />
                <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                ConnectionString="<%$ ConnectionStrings:MemberConnectionString %>"

            SelectCommand="SELECT [Member_Info].[Member_ID], [Member_Info].[First_Name], [Member_Info].[Last_Name] FROM [Member_Info] inner join [Membermaster] on [Member_Info].[Member_ID]=[Membermaster].[Member_ID]"></asp:SqlDataSource>



          <asp:Button ID="btsave" runat="server" onclick="btsave_Click" Text="Save" />



          </div>



</asp:Content>


====================================
C#
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;

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

        }

        protected void btsave_Click(object sender, EventArgs e)
        {

            string Member_ID = string.Empty;
            //string Frist_Name = string.Empty;
            //string Last_Name = string.Empty;
           string TextBox1=string.Empty;
            //int collection = Convert.ToInt32(TextBox1);
            //int TextBox1 = Convert.ToInt32(TextBox1.Text).ToString();
            //collection = Convert.ToInt32(TextBox1.Text);



            int GVCount = GridView1.Rows.Count;

            foreach (GridViewRow GVRow in GridView1.Rows)
            {
                Member_ID = GVRow.Cells[0].Text;
                // Frist_Name = GVRow.Cells[1].Text;
                //Last_Name = GVRow.Cells[2].Text;
                TextBox1 = GVRow.Cells[3].Text;
                
                //int Mobile = int.Parse(tbmobileno.Text);


                SqlConnection myConnection = new SqlConnection("user id=sa;" +
                                           "password=prodip@km;server=PRODIP;" +
                                           "Trusted_Connection=yes;" +
                                           "database=Member;");
                myConnection.Open();

                SqlCommand myCommand = new SqlCommand("INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)" +
    "Values(@Member_ID,@TextBox1,GETDATE())");

                //int Member_ID = int.Parse(GVRow.Cells[1].Text);


                myCommand.Parameters.Add("@Member_ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
                myCommand.Parameters.Add("@TextBox1", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());
                //myCommand.Parameters.AddWithValue("@TextBox1", TextBox1.ToString());
                
                myCommand.Connection = myConnection;
                myCommand.ExecuteNonQuery();

                Response.Write("<script language='javascript'>{alert('" + "Data is Save')}</script>");

                myConnection.Close();

            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

       
    }
}


..........Please help;;;;;;;;;;;;;;;;;;
Posted
Updated 2-Apr-13 19:45pm
v2
Comments
Naz_Firdouse 3-Apr-13 1:51am    
are you getting the value at TextBox1 = GVRow.Cells[3].Text;
if not , use this...
TextBox1 = (GVRow.FindControl("TextBox1") as TextBox).Text;
prodipjsr 3-Apr-13 5:15am    
Thank ypu Naz_Firdouse,
how can i convert the TextBox string values to integer?? because the data type in my data base id int32. i am using "((TextBox)GVRow.FindControl("TextBox1")).Text;" code but it shown error. help me please.how i do this.

1 solution

TextBox1 is in your ItemTemplate, You need to find the TextBox to get it's value.
Replace your loop with this:
C#
foreach (GridViewRow GVRow in GridView1.Rows)
{
    Member_ID = GVRow.Cells[0].Text;
    TextBox1 = ((TextBox)GVRow.FindControl("TextBox1")).Text;    
    SqlConnection myConnection = new SqlConnection("user id=sa;" +
                               "password=prodip@km;server=PRODIP;" +
                               "Trusted_Connection=yes;" +
                               "database=Member;");
    myConnection.Open();
    SqlCommand myCommand = new SqlCommand("INSERT INTO [Collectionamt ] (Member_ID,collectionamt,collectiondate)" +
"Values(@Member_ID,@TextBox1,GETDATE())");
    myCommand.Parameters.Add("@Member_ID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(Member_ID);
    myCommand.Parameters.Add("@TextBox1", System.Data.SqlDbType.VarChar,50).Value = (TextBox1.ToString());    
    myCommand.Connection = myConnection;
    myCommand.ExecuteNonQuery();
    Response.Write("<script language="'javascript'">{alert('" + "Data is Save')}</script>");
    myConnection.Close();
}




--Amit
 
Share this answer
 
Comments
prodipjsr 3-Apr-13 2:08am    
Thank you so much Amit. Its working fine. But one small problem still happened. The problem is ..last member_ID is inserted two time in every click. how i solve it??
_Amy 3-Apr-13 2:11am    
If your problem solved then mark it as solution. Now you'll have to debug your code. Put the breakpoint and and check what you are passing to database.
prodipjsr 3-Apr-13 5:16am    
Thank you amy,
how can i convert the TextBox string values to integer?? because the data type in my data base id int32. i am using "((TextBox)GVRow.FindControl("TextBox1")).Text;" code but it shown error. help me please. please ans me..
_Amy 3-Apr-13 5:18am    
Try Convert.ToInt32(((TextBox)GVRow.FindControl("TextBox1")).Text);.
prodipjsr 3-Apr-13 5:29am    
Not working. showing error mess "The name Try does not cxist in current contex"..how to solve please.....

my code is:::TextBox1 = Try Convert.ToInt32(((TextBox)GVRow.FindControl("TextBox1")).Text);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900