Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7643171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:22:52+00:00 2026-05-31T09:22:52+00:00

I have a gridview which is being populated at runtime(using sessions) by entering the

  • 0
I have a gridview which is being populated at runtime(using sessions) by entering the respective values in the web form. 
Now i want to retrieve all the data from that gridview and fill it into my table(MySql table)..

How can i do that??? 

//creating datatable

 private DataTable CreateDataTable2()
    {
        DataTable myDataTable2 = new DataTable();
        DataColumn myDataColumn;

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Sim Imei";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Mobile Number";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Sim ID";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "IssuedForAccount";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "IssuedForDevice";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Device Imei";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Issued By";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Invoice Number";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Invoice Date";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Issued To";
        myDataTable2.Columns.Add(myDataColumn);

        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Authority Name";
        myDataTable2.Columns.Add(myDataColumn);


        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Company Name";
        myDataTable2.Columns.Add(myDataColumn);


        myDataColumn = new DataColumn();
        myDataColumn.DataType = Type.GetType("System.String");
        myDataColumn.ColumnName = "Company Address";
        myDataTable2.Columns.Add(myDataColumn);




        return myDataTable2;
    }
//creating function to add data
private void AddDataToTable2(string SimImei, string MobileNumber, string SimID, string  IssuedForAccount, string IssuedForDevice, string DeviceImei, string IssuedBy, string InvoiceNumber, string InvoiceDate, string IssuedTo, string AuthorityName, string CompanyName, string Address, DataTable myTable)
    {
        DataRow row = myTable.NewRow();
        row["Sim Imei"] = SimImei;
        row["Mobile Number"] = MobileNumber;
        row["Sim ID"] = SimID;
        row["IssuedForAccount"] = IssuedForAccount;
        row["IssuedForDevice"] = IssuedForDevice;
        row["Device Imei"] = DeviceImei;
        row["Issued By"] = IssuedBy;
        row["Invoice Number"] = InvoiceNumber;
        row["Invoice Date"] = InvoiceDate;
        row["Issued To"] = IssuedTo;
        row["Authority Name"] = AuthorityName;
        row["Company Name"] = CompanyName;
        row["Company Address"] = Address;
        myTable.Rows.Add(row);
    }

 public static void ShowAlertMessage(string error)
    {
        Page page = HttpContext.Current.Handler as Page;
        if (page != null)
        {
            error = error.Replace("'", "\'");
            ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
        }
    }

//button on click the values are enetered into the gridview

protected void btn_InsertSim_Click(object sender, EventArgs e)
    {
        reporting tableValues = new reporting();

        int t = 0;
        int price = 0;

        try
        {

            #region Insert values in gridview
            connection.Open();
            string devicetyp = null;
            string mobile = null;

            try
            {


                string mobiledata="select MobileNumber from siminventoryin where  SimImei='"+txt_SubjectIMEI.Text+"'";
                MySqlCommand cmd5=new MySqlCommand (mobiledata,connection);
                cmd5.ExecuteNonQuery();
                MySqlDataReader rdr=cmd5.ExecuteReader();
                while (rdr.Read())
                {
                     mobile = (string)rdr["MobileNumber"];
                }
                rdr.Close();


                string devicedata = "select Devicetype from deviceinventoryin where                  DeviceIMEI='" + ddl_adjoiningdeviceSerial.SelectedValue.ToString() + "'";
                MySqlCommand cmd6 = new MySqlCommand(devicedata, connection);
                cmd6.ExecuteNonQuery();
                MySqlDataReader rd = cmd6.ExecuteReader();
                while (rd.Read())
                {
                    devicetyp = (string)rd["DeviceType"];
                }
                rd.Close();
                AddDataToTable2(txt_SubjectIMEI.Text, mobile, txt_SubjectID.Text, ddl_accountid.SelectedValue, devicetyp, ddl_adjoiningdeviceSerial.SelectedValue, txt_issuedby.Text, txt_invoiceno.Text, txt_invoicedate.Text, txt_issuedto.Text, txt_name.Text, txt_compname.Text, txt_address.Text, (DataTable)Session["myDatatable2"]);


                grd_showsim.DataSource = ((DataTable)Session["myDatatable2"]).DefaultView;
                grd_showsim.DataBind();



            }
            catch (Exception ex)
            {
                ShowAlertMessage(ex.Message.ToString());
            }
            finally
            {

            }
            #endregion
        }
        catch
        {
            ShowAlertMessage("Oops, Some error occured... We are working on it");
        }
        finally
        {
            connection.Close();
            //txt_SubjectID.Text = "";
            //txt_SubjectIMEI.Text = "";

        }
    }
    #endregion 

//databound of gridview

protected void grd_showsim_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblSerial = (Label)e.Row.FindControl("lblSerial");
lblSerial.Text = i.ToString();
i++;
}
}

Please help…!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T09:22:53+00:00Added an answer on May 31, 2026 at 9:22 am

    You can run a foreach on your gridview control & reference each individual Template Control or by using Eval. Something like

    try{
     foreach (GridViewRow gr in YourGridView.Rows)
     {
        TextBox tb1 = ((TextBox) gr.FindControl("YourControlId1")).Text;
        TextBox tb2 = ((TextBox) gr.FindControl("YourControlId2")).Text;
        // Insert to Database here
        // Open Connection ; ExecuteNonQuery();
      }
    }
    catch(Exception ex)
    {
       // log exception
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a gridview which is databound, I want to dynamically add a row
Have a gridview control which will show X amount of rows. I want to
I have a script which loops through a GridView on my web page, and
I have an ASP.NET GridView which is populated from a SQL Server database. This
I have a grid view which is populated using a custom ImageAdapter class extending
I have a gridview which I want to zebra stripe after it has been
I have a gridview which can be filtered from one or more values in
I have a gridview that is being populated from a datasouce. The Stored procedure
I have an application that uses an aps:GridView. The GridView is populated using a
I have a GridView which is populated from a database, and includes a textbox.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.