Fetch value from database table and save all these values to another table in asp.net datagridview

Last Reply 11 days ago By Shivam.M

Posted 12 days ago

I have a following table tblData

DamID

CalfTag

B_Type

Bread

Loctation

101

1010

H

5

0

102

1020

H

5

0

103

1030

H

5

0

from above table I displayed few columns in the data grid view like that

DamID

CalfTag

B_Type

Action

101

1010

H

Update

102

1020

H

Update

103

1030

H

Update

Requirements

When I click on Update link button in each inside datagrid view, then it will fetch all five values from above tblData and will save all the fetched values in another table which is following like that

tblAnimal

DamName

TagID

B_Type

Bread

LocNo

101

1010

H

5

0

103

1030

H

5

0

 How to get solution pls. here is my code

                                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ForeColor="#333333" 
                                        RowStyle-Wrap="false" HeaderStyle-Wrap="false"
                                        Class="table table-striped table-bordered table-hover" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                                        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" 
                                        onrowdatabound="GridView1_RowDataBound" >
                                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <Columns>
                                            <asp:TemplateField>
                                                <HeaderTemplate>
                                                    <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Calf ID">
                                                <ItemTemplate>
                                                    <asp:Label ID="lbl_No" runat="server" Text='<%# Eval("CalfID") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Dam ID">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DamID") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtDam" runat="server" Text='<%# Eval("DamID") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Sire Name">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("SireName") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtSire" runat="server" Text='<%# Eval("SireName") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Date of Birth">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("DOB") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            
                                            <asp:TemplateField HeaderText="Calf Tag">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("CalfTag") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtCalfTag" runat="server" Text='<%# Eval("CalfTag") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            
                                             <asp:TemplateField HeaderText="Gender">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("Gender") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtGender" runat="server" Text='<%# Eval("Gender") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                              <asp:TemplateField HeaderText="Weight">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label6" runat="server" Text='<%# Eval("Weight") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtWeight" runat="server" Text='<%# Eval("Weight") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                             <asp:TemplateField HeaderText="Pen Name ">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                                </ItemTemplate>                                             
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Total Days ">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label70" runat="server" Text='<%# Eval("Days") %>'></asp:Label>
                                                </ItemTemplate>                                             
                                            </asp:TemplateField>

                                            <asp:TemplateField HeaderText="Action" ShowHeader="false">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Update" Visible ='<%# Convert.ToInt16(Eval("Days")) >= Convert.ToInt16(91) %>'></asp:LinkButton>                                                   
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Heifer"></asp:LinkButton>
                                                    <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Dead"></asp:LinkButton>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                           
                                        </Columns>
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <EditRowStyle BackColor="#CCCCCC" />
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    </asp:GridView>

 

                                    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" ForeColor="#333333" 
                                        RowStyle-Wrap="false" HeaderStyle-Wrap="false"
                                        Class="table table-striped table-bordered table-hover" OnRowCancelingEdit="GridView1_RowCancelingEdit"
                                        OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating" 
                                        onrowdatabound="GridView1_RowDataBound" >
                                        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                                        <Columns>
                                            <asp:TemplateField>
                                                <HeaderTemplate>
                                                    <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
                                                </HeaderTemplate>
                                                <ItemTemplate>
                                                    <asp:CheckBox ID="CheckBox1" runat="server" onclick="Check_Click(this)" />
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Calf ID">
                                                <ItemTemplate>
                                                    <asp:Label ID="lbl_No" runat="server" Text='<%# Eval("CalfID") %>'></asp:Label>
                                                </ItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Dam ID">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("DamID") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtDam" runat="server" Text='<%# Eval("DamID") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                           <asp:TemplateField HeaderText="Sire Name">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label2" runat="server" Text='<%# Eval("SireName") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtSire" runat="server" Text='<%# Eval("SireName") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Date of Birth">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label3" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtDate" runat="server" Text='<%# Eval("DOB") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            
                                            <asp:TemplateField HeaderText="Calf Tag">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label4" runat="server" Text='<%# Eval("CalfTag") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtCalfTag" runat="server" Text='<%# Eval("CalfTag") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>
                                            
                                             <asp:TemplateField HeaderText="Gender">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label5" runat="server" Text='<%# Eval("Gender") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtGender" runat="server" Text='<%# Eval("Gender") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                              <asp:TemplateField HeaderText="Weight">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label6" runat="server" Text='<%# Eval("Weight") %>'></asp:Label>
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:TextBox ID="txtWeight" runat="server" Text='<%# Eval("Weight") %>' class="form-control"
                                                        Width="150"></asp:TextBox>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                             <asp:TemplateField HeaderText="Pen Name ">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label7" runat="server" Text='<%# Eval("Name") %>'></asp:Label>
                                                </ItemTemplate>                                             
                                            </asp:TemplateField>
                                            <asp:TemplateField HeaderText="Total Days ">
                                                <ItemTemplate>
                                                    <asp:Label ID="Label70" runat="server" Text='<%# Eval("Days") %>'></asp:Label>
                                                </ItemTemplate>                                             
                                            </asp:TemplateField>

                                            <asp:TemplateField HeaderText="Action" ShowHeader="false">
                                                <ItemTemplate>
                                                    <asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Update" Visible ='<%# Convert.ToInt16(Eval("Days")) >= Convert.ToInt16(91) %>'></asp:LinkButton>                                                   
                                                </ItemTemplate>
                                                <EditItemTemplate>
                                                    <asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Heifer"></asp:LinkButton>
                                                    <asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Dead"></asp:LinkButton>
                                                </EditItemTemplate>
                                            </asp:TemplateField>

                                           
                                        </Columns>
                                        <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                                        <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                                        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <EditRowStyle BackColor="#CCCCCC" />
                                        <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                                    </asp:GridView>
You are viewing reply posted by: Shivam.M 11 days ago.
Posted 11 days ago

Hi smile,

Please refer below sample.

HTML

<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="DamID" HeaderText="Id" />
        <asp:BoundField DataField="CalfTag" HeaderText="CalfTag" />
        <asp:BoundField DataField="B_Type" HeaderText="B_Type" />
        <asp:TemplateField HeaderText="Action">
            <ItemTemplate>
                <asp:LinkButton ID="lnkUpdate" runat="server" Text="Update" OnClick="OnUpdate"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<br />
<asp:GridView ID="gvAnimals" runat="server"></asp:GridView>

Namespaces

C#

using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        this.BindGridView();
    }
}

protected void OnUpdate(object sender, EventArgs e)
{
    List<int> damids = new List<int>();
    GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
    int damId = int.Parse(row.Cells[0].Text);
    if (Session["Id"] == null)
    {
        damids.Add(damId);
        Session["Id"] = damids;
    }
    else
    {
        damids = (List<int>)Session["Id"];
        damids.Add(damId);
        Session["Id"] = damids;
    }
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand(string.Format("SELECT * FROM tblData WHERE DamID IN ({0})", string.Join(",", damids.Distinct())), con))
        {
            con.Open();
            gvAnimals.DataSource = cmd.ExecuteReader();
            gvAnimals.DataBind();
            con.Close();
        }
    }
}

private void BindGridView()
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT DamID, CalfTag, B_Type FROM tblData", con))
        {
            con.Open();
            gvDetails.DataSource = cmd.ExecuteReader();
            gvDetails.DataBind();
            con.Close();
        }
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Me.BindGridView()
    End If
End Sub

Protected Sub OnUpdate(ByVal sender As Object, ByVal e As EventArgs)
    Dim damids As List(Of Integer) = New List(Of Integer)()
    Dim row As GridViewRow = TryCast((TryCast(sender, LinkButton)).NamingContainer, GridViewRow)
    Dim damId As Integer = Integer.Parse(row.Cells(0).Text)

    If Session("Id") Is Nothing Then
        damids.Add(damId)
        Session("Id") = damids
    Else
        damids = CType(Session("Id"), List(Of Integer))
        damids.Add(damId)
        Session("Id") = damids
    End If

    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand(String.Format("SELECT * FROM tblData WHERE DamID IN ({0})", String.Join(",", damids.Distinct())), con)
            con.Open()
            gvAnimals.DataSource = cmd.ExecuteReader()
            gvAnimals.DataBind()
            con.Close()
        End Using
    End Using
End Sub

Private Sub BindGridView()
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand("SELECT DamID, CalfTag, B_Type FROM tblData", con)
            con.Open()
            gvDetails.DataSource = cmd.ExecuteReader()
            gvDetails.DataBind()
            con.Close()
        End Using
    End Using
End Sub

Screenshot