C#.NET - Loop through <asp:tablerow>

scross1276
scross1276 used Ask the Experts™
on
I'm trying to figure out how to loop through the rows of an ASP.NET table and insert a record into the database for each row.

Any help is incredibly appreciated!!


(There are currently eight identical rows in my table, hardcoded as, e.g., txtReportName1, txtReportName2, etc.  I hope to change that to dynamically added rows based on a button click later, just an FYI.)
ASP.NET Code:
 
<asp:Table ID="tblRevealReports" runat="server" CellSpacing="2" Width="750px" BorderWidth="0"
            Style="border-color: #000000;">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>Report Name</asp:TableHeaderCell>
                <asp:TableHeaderCell>Report Description</asp:TableHeaderCell>
                <asp:TableHeaderCell>Frequency</asp:TableHeaderCell>
                <asp:TableHeaderCell>Database #</asp:TableHeaderCell>
                <asp:TableHeaderCell>Format</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportName1" runat="server" MaxLength="30" /></asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportDesc1" runat="server" Width="250px" MaxLength="50" />
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlFrequency1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Daily</asp:ListItem>
                        <asp:ListItem Value="2">Weekly</asp:ListItem>
                        <asp:ListItem Value="3">Monthly</asp:ListItem>
                        <asp:ListItem Value="4">Quarterly</asp:ListItem>
                        <asp:ListItem Value="5">Annually</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlDatabase1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">10 - TMP</asp:ListItem>
                        <asp:ListItem Value="2">15 - TMP LIFO</asp:ListItem>
                        <asp:ListItem Value="3">30 - PPD</asp:ListItem>
                        <asp:ListItem Value="4">35 - PPD LIFO</asp:ListItem>
                        <asp:ListItem Value="5">Invoice File</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlAccess1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Print</asp:ListItem>
                        <asp:ListItem Value="2">Reveal</asp:ListItem>
                        <asp:ListItem Value="3">Both</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
            </asp:TableRow>
</asp:table>
 
 
C#.NET Code:
 
string strInsertRevealDetails;
 
            foreach (TableRow item in tblRevealReports)
            {
                strInsertRevealDetails = "insert into table (name, description, freq, db, accessType, rid)" +
                    "VALUES (@name, @description, @freq, @db, @accessType, @rid)";
                SqlCommand cmdInsertRevealDetails = new SqlCommand(strInsertRevealDetails, myConnection);
                cmdInsertRevealDetails.Parameters.AddWithValue("@name", txtReportName1.Text);
            }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
this sample help you.

change in paramteter dbtype as per your requirement.



<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Table ID="tblRevealReports" runat="server" CellSpacing="2" Width="750px" BorderWidth="0"
            Style="border-color: #000000;">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>Report Name</asp:TableHeaderCell>
                <asp:TableHeaderCell>Report Description</asp:TableHeaderCell>
                <asp:TableHeaderCell>Frequency</asp:TableHeaderCell>
                <asp:TableHeaderCell>Database #</asp:TableHeaderCell>
                <asp:TableHeaderCell>Format</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportName1" runat="server" MaxLength="30" /></asp:TableCell><asp:TableCell>
                    <asp:TextBox ID="txtReportDesc1" runat="server" Width="250px" MaxLength="50" />
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlFrequency1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Daily</asp:ListItem>
                        <asp:ListItem Value="2">Weekly</asp:ListItem>
                        <asp:ListItem Value="3">Monthly</asp:ListItem>
                        <asp:ListItem Value="4">Quarterly</asp:ListItem>
                        <asp:ListItem Value="5">Annually</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlDatabase1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">10 - TMP</asp:ListItem>
                        <asp:ListItem Value="2">15 - TMP LIFO</asp:ListItem>
                        <asp:ListItem Value="3">30 - PPD</asp:ListItem>
                        <asp:ListItem Value="4">35 - PPD LIFO</asp:ListItem>
                        <asp:ListItem Value="5">Invoice File</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlAccess1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Print</asp:ListItem>
                        <asp:ListItem Value="2">Reveal</asp:ListItem>
                        <asp:ListItem Value="3">Both</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell></asp:TableRow>
                 <asp:TableRow>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportName2" runat="server" MaxLength="30" /></asp:TableCell><asp:TableCell>
                    <asp:TextBox ID="txtReportDesc2" runat="server" Width="250px" MaxLength="50" />
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlFrequency2" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Daily</asp:ListItem>
                        <asp:ListItem Value="2">Weekly</asp:ListItem>
                        <asp:ListItem Value="3">Monthly</asp:ListItem>
                        <asp:ListItem Value="4">Quarterly</asp:ListItem>
                        <asp:ListItem Value="5">Annually</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlDatabase2" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">10 - TMP</asp:ListItem>
                        <asp:ListItem Value="2">15 - TMP LIFO</asp:ListItem>
                        <asp:ListItem Value="3">30 - PPD</asp:ListItem>
                        <asp:ListItem Value="4">35 - PPD LIFO</asp:ListItem>
                        <asp:ListItem Value="5">Invoice File</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell><asp:TableCell>
                    <asp:DropDownList ID="ddlAccess2" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Print</asp:ListItem>
                        <asp:ListItem Value="2">Reveal</asp:ListItem>
                        <asp:ListItem Value="3">Both</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell></asp:TableRow>
                 
                </asp:table></div><asp:Button 
        ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /></form>
</body>
</html>
 
 
 
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;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmdInsertRevealDetails = new SqlCommand();
        string strInsertRevealDetails = "insert into table (name, description, freq, db, accessType, rid)" +
             "VALUES (@name, @description, @freq, @db, @accessType, @rid)";
        cmdInsertRevealDetails.CommandText = strInsertRevealDetails;
        cmdInsertRevealDetails.Connection = con;
        cmdInsertRevealDetails.Parameters.Add("@name", System.Data.SqlDbType.NVarChar,50);
        cmdInsertRevealDetails.Parameters.Add("@description", System.Data.SqlDbType.NVarChar, 500);
        cmdInsertRevealDetails.Parameters.Add("@freq", System.Data.SqlDbType.Int);
        cmdInsertRevealDetails.Parameters.Add("@db", System.Data.SqlDbType.Int);
        cmdInsertRevealDetails.Parameters.Add("@accesstype", System.Data.SqlDbType.Int);
        cmdInsertRevealDetails.Parameters.Add("@rid", System.Data.SqlDbType.Int);
        int indexcontrol = 1;
        foreach (TableRow item in tblRevealReports.Rows)
        {
            TextBox reportName = item.FindControl("txtReportName" + indexcontrol.ToString()) as TextBox ;
            TextBox reportDesc = item.FindControl("txtReportDesc" + indexcontrol.ToString()) as TextBox;
            DropDownList ddlFreq = item.FindControl("ddlFrequency" + indexcontrol.ToString()) as  DropDownList;
            DropDownList ddlDb = item.FindControl("ddlDatabase" + indexcontrol.ToString()) as DropDownList;
            DropDownList ddlAccess = item.FindControl("ddlAccess" + indexcontrol.ToString()) as DropDownList;
            cmdInsertRevealDetails.Parameters["@name"].Value = reportName.Text;
            cmdInsertRevealDetails.Parameters["@description"].Value = reportDesc.Text;
            cmdInsertRevealDetails.Parameters["@freq"].Value = int.Parse(ddlFreq.SelectedValue);
            cmdInsertRevealDetails.Parameters["@db"].Value = int.Parse(ddlDb.SelectedValue);
            cmdInsertRevealDetails.Parameters["@accesstype"].Value = int.Parse(ddlDb.SelectedValue);
            cmdInsertRevealDetails.Parameters["@rid"].Value = indexcontrol;
            cmdInsertRevealDetails.ExecuteNonQuery();
            indexcontrol++;        
        }
    }
}

Open in new window

Author

Commented:
I'm getting "object reference not set to instance of an object" on

cmdInsertRevealDetails.Parameters["@name"].Value = reportName.Text;

Author

Commented:
Here's how my code looks.  (No changes have been made to the aspx side.)
string strInsertRevealDetails = "insert into table (name, description, freq, db, accessType, rid)" +
                    "VALUES (@name, @description, @freq, @db, @accessType, @rid)";
            SqlCommand cmdInsertRevealDetails = new SqlCommand(strInsertRevealDetails, myConnection);
 
            cmdInsertRevealDetails.Parameters.Add("@name", System.Data.SqlDbType.VarChar, 30);
            cmdInsertRevealDetails.Parameters.Add("@description", System.Data.SqlDbType.VarChar, 50);
            cmdInsertRevealDetails.Parameters.Add("@freq", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@db", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@accesstype", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@rid", System.Data.SqlDbType.Int);
            int indexcontrol = 1;
 
            foreach (TableRow item in tblRevealReports.Rows)
            {
            TextBox reportName = item.FindControl("txtReportName1" + indexcontrol.ToString()) as TextBox;
            TextBox reportDesc = item.FindControl("txtReportDesc1" + indexcontrol.ToString()) as TextBox;
            DropDownList ddlFreq = item.FindControl("ddlFrequency1" + indexcontrol.ToString()) as  DropDownList;
            DropDownList ddlDb = item.FindControl("ddlDatabase1" + indexcontrol.ToString()) as DropDownList;
            DropDownList ddlAccess = item.FindControl("ddlAccess1" + indexcontrol.ToString()) as DropDownList;
            cmdInsertRevealDetails.Parameters["@name"].Value = reportName.Text;
            cmdInsertRevealDetails.Parameters["@description"].Value = reportDesc.Text;
            cmdInsertRevealDetails.Parameters["@freq"].Value = int.Parse(ddlFreq.SelectedValue);
            cmdInsertRevealDetails.Parameters["@db"].Value = int.Parse(ddlDb.SelectedValue);
            cmdInsertRevealDetails.Parameters["@accesstype"].Value = int.Parse(ddlAccess.SelectedValue);
            cmdInsertRevealDetails.Parameters["@rid"].Value = indexcontrol;
            cmdInsertRevealDetails.ExecuteNonQuery();
            indexcontrol++;
            }

Open in new window

CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

Commented:
please put your code over here that u used.  Set connection string , con.open() in my code.

i need your aspx part. What is control name in other row. Try to give control name in sequence.

Author

Commented:

aspx snippet:
 
<asp:Table ID="tblRevealReports" runat="server" CellSpacing="2" Width="750px" BorderWidth="0"
            Style="border-color: #000000;">
            <asp:TableHeaderRow>
                <asp:TableHeaderCell>Report Name</asp:TableHeaderCell>
                <asp:TableHeaderCell>Report Description</asp:TableHeaderCell>
                <asp:TableHeaderCell>Frequency</asp:TableHeaderCell>
                <asp:TableHeaderCell>Database #</asp:TableHeaderCell>
                <asp:TableHeaderCell>Format</asp:TableHeaderCell>
            </asp:TableHeaderRow>
            <asp:TableRow>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportName1" runat="server" MaxLength="30" /></asp:TableCell>
                <asp:TableCell>
                    <asp:TextBox ID="txtReportDesc1" runat="server" Width="250px" MaxLength="50" />
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlFrequency1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Daily</asp:ListItem>
                        <asp:ListItem Value="2">Weekly</asp:ListItem>
                        <asp:ListItem Value="3">Monthly</asp:ListItem>
                        <asp:ListItem Value="4">Quarterly</asp:ListItem>
                        <asp:ListItem Value="5">Annually</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlDatabase1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">10 - TMP</asp:ListItem>
                        <asp:ListItem Value="2">15 - TMP LIFO</asp:ListItem>
                        <asp:ListItem Value="3">30 - PPD</asp:ListItem>
                        <asp:ListItem Value="4">35 - PPD LIFO</asp:ListItem>
                        <asp:ListItem Value="5">Invoice File</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
                <asp:TableCell>
                    <asp:DropDownList ID="ddlAccess1" runat="server" Width="100px">
                        <asp:ListItem Value="0"><--Select--></asp:ListItem>
                        <asp:ListItem Value="1">Print</asp:ListItem>
                        <asp:ListItem Value="2">Reveal</asp:ListItem>
                        <asp:ListItem Value="3">Both</asp:ListItem>
                    </asp:DropDownList>
                </asp:TableCell>
            </asp:TableRow>

Open in new window

Commented:

you are doing this.
 
TextBox reportName = item.FindControl("txtReportName1" + indexcontrol.ToString()) as TextBox;
            TextBox reportDesc = item.FindControl("txtReportDesc1" + indexcontrol.ToString()) as TextBox;
            DropDownList ddlFreq = item.FindControl("ddlFrequency1" + indexcontrol.ToString()) as  DropDownList;
            DropDownList ddlDb = item.FindControl("ddlDatabase1" + indexcontrol.ToString()) as DropDownList;
            DropDownList ddlAccess = item.FindControl("ddlAccess1" + indexcontrol.ToString()) as DropDownList;
 
 
do this.
 
TextBox reportName = item.FindControl("txtReportName" + indexcontrol.ToString()) as TextBox;
            TextBox reportDesc = item.FindControl("txtReportDesc" + indexcontrol.ToString()) as TextBox;
            DropDownList ddlFreq = item.FindControl("ddlFrequency" + indexcontrol.ToString()) as  DropDownList;
            DropDownList ddlDb = item.FindControl("ddlDatabase" + indexcontrol.ToString()) as DropDownList;
            DropDownList ddlAccess = item.FindControl("ddlAccess" + indexcontrol.ToString()) as DropDownList;

Open in new window

Commented:
you are adding  item.FindControl("txtReportName1"+indexcontrol.ToString())

instead of that do

item.FindControl("txtReportName"+indexcontrol.ToString())

b'coz i am creating id dynamic so you can get record from many rows in loop.

Author

Commented:
I still get the same error on the same line.
Commented:

        myConnection.Open();
            string strInsertRevealDetails = "insert into table (name, description, freq, db, accessType, rid)" +
                    "VALUES (@name, @description, @freq, @db, @accessType, @rid)";
            SqlCommand cmdInsertRevealDetails = new SqlCommand(strInsertRevealDetails, myConnection);
 
            cmdInsertRevealDetails.Parameters.Add("@name", System.Data.SqlDbType.VarChar, 30);
            cmdInsertRevealDetails.Parameters.Add("@description", System.Data.SqlDbType.VarChar, 50);
            cmdInsertRevealDetails.Parameters.Add("@freq", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@db", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@accesstype", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@rid", System.Data.SqlDbType.Int);
            int indexcontrol = 1;
 
            foreach (TableRow item in tblRevealReports.Rows)
            {
            TextBox reportName = item.FindControl("txtReportName" + indexcontrol.ToString()) as TextBox;
            TextBox reportDesc = item.FindControl("txtReportDesc" + indexcontrol.ToString()) as TextBox;
            DropDownList ddlFreq = item.FindControl("ddlFrequency" + indexcontrol.ToString()) as  DropDownList;
            DropDownList ddlDb = item.FindControl("ddlDatabase" + indexcontrol.ToString()) as DropDownList;
            DropDownList ddlAccess = item.FindControl("ddlAccess" + indexcontrol.ToString()) as DropDownList;
            cmdInsertRevealDetails.Parameters["@name"].Value = reportName.Text;
            cmdInsertRevealDetails.Parameters["@description"].Value = reportDesc.Text;
            cmdInsertRevealDetails.Parameters["@freq"].Value = int.Parse(ddlFreq.SelectedValue);
            cmdInsertRevealDetails.Parameters["@db"].Value = int.Parse(ddlDb.SelectedValue);
            cmdInsertRevealDetails.Parameters["@accesstype"].Value = int.Parse(ddlAccess.SelectedValue);
            cmdInsertRevealDetails.Parameters["@rid"].Value = indexcontrol;
            cmdInsertRevealDetails.ExecuteNonQuery();
            indexcontrol++;
            }

Open in new window

Author

Commented:
FYI - I'm opening my connection prior to doing an insert into a different table earlier in the code.

Same error on same line though.

Commented:
put your final code.

Author

Commented:

protected void btnSave_Click(Object sender, EventArgs e)
        {
            txtApprovingSupervisor.Text = ddlApprovingSupervisor.SelectedItem.Text;
            txtApproverID.Text = ddlApprovingSupervisor.SelectedValue.ToString();
 
            string strInsertReveal;
            SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["***"].ConnectionString);
            myConnection.Open();
 
            strInsertReveal = "insert into table1(requestDate, requestedBy, newUser, newUserid, positionLocation, salesman, userMirror, approvingSupervisor, savedBy, savedOn, approved)" +
                "VALUES (@requestDate, @requestedBy, @newUser, @newUserid, @positionLocation, @salesman, @userMirror, @approvingSupervisor, @savedBy, @savedOn, @approved)";
            SqlCommand cmdInsertReveal = new SqlCommand(strInsertReveal, myConnection);
            cmdInsertReveal.Parameters.AddWithValue("@requestDate", lblRequestDate.Text);
            cmdInsertReveal.Parameters.AddWithValue("@requestedBy", lblRequestor.Text);
            cmdInsertReveal.Parameters.AddWithValue("@newUser", txtName.Text);
            cmdInsertReveal.Parameters.AddWithValue("@newUserid", txtID.Text);
            cmdInsertReveal.Parameters.AddWithValue("@positionLocation", txtPositionLocation.Text);
            cmdInsertReveal.Parameters.AddWithValue("@salesman", txtSalesman.Text);
            cmdInsertReveal.Parameters.AddWithValue("@userMirror", txtMirror.Text);
            cmdInsertReveal.Parameters.AddWithValue("@approvingSupervisor", txtApprovingSupervisor.Text);
            cmdInsertReveal.Parameters.AddWithValue("@savedBy", GetUserName());
            cmdInsertReveal.Parameters.AddWithValue("@savedOn", System.DateTime.Now);
            cmdInsertReveal.Parameters.AddWithValue("@approved", "No");
            cmdInsertReveal.ExecuteNonQuery();
 
 
            string strRID = "select id from table1 where id = @@identity";
            SqlCommand cmdRID = new SqlCommand(strRID, myConnection);
            SqlDataReader sdrRID = cmdRID.ExecuteReader(CommandBehavior.SingleRow);
            if (sdrRID.Read())
            {
                txtRevealID.Text = sdrRID["id"].ToString();
            }
            sdrRID.Close();
 
 
 
            string strInsertRevealDetails = "insert into table2 (name, description, freq, db, accessType, rid)" +
                    "VALUES (@name, @description, @freq, @db, @accessType, @rid)";
            SqlCommand cmdInsertRevealDetails = new SqlCommand(strInsertRevealDetails, myConnection);
 
            cmdInsertRevealDetails.Parameters.Add("@name", System.Data.SqlDbType.VarChar, 30);
            cmdInsertRevealDetails.Parameters.Add("@description", System.Data.SqlDbType.VarChar, 50);
            cmdInsertRevealDetails.Parameters.Add("@freq", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@db", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@accesstype", System.Data.SqlDbType.VarChar, 15);
            cmdInsertRevealDetails.Parameters.Add("@rid", System.Data.SqlDbType.Int);
            int indexcontrol = 1;
 
            foreach (TableRow item in tblRevealReports.Rows)
            {
                TextBox reportName = item.FindControl("txtReportName" + indexcontrol.ToString()) as TextBox;
                TextBox reportDesc = item.FindControl("txtReportDesc" + indexcontrol.ToString()) as TextBox;
                DropDownList ddlFreq = item.FindControl("ddlFrequency" + indexcontrol.ToString()) as DropDownList;
                DropDownList ddlDb = item.FindControl("ddlDatabase" + indexcontrol.ToString()) as DropDownList;
                DropDownList ddlAccess = item.FindControl("ddlAccess" + indexcontrol.ToString()) as DropDownList;
                cmdInsertRevealDetails.Parameters["@name"].Value = reportName.Text;
                cmdInsertRevealDetails.Parameters["@description"].Value = reportDesc.Text;
                cmdInsertRevealDetails.Parameters["@freq"].Value = int.Parse(ddlFreq.SelectedValue);
                cmdInsertRevealDetails.Parameters["@db"].Value = int.Parse(ddlDb.SelectedValue);
                cmdInsertRevealDetails.Parameters["@accesstype"].Value = int.Parse(ddlAccess.SelectedValue);
                cmdInsertRevealDetails.Parameters["@rid"].Value = indexcontrol;
                cmdInsertRevealDetails.ExecuteNonQuery();
                indexcontrol++;
            }
 
 
myConnection.Close();
 
            Response.Redirect("notifyApprover.aspx");
        }

Open in new window

Commented:
i think it should work . As my side it is working.

which framework ?

Is it return error at first glance ?

Commented:
means at first loop return error.

Author

Commented:
2.0 Framework.  The error comes back after the button is clicked, not on load.

Commented:
are you removing or adding row to table dynamically.

b'coz if you see my first comment it is same as yours.

Author

Commented:
I don't have the dynamic add row code working yet.

FYI - I just stepped through the code in debug mode.  It works fine until it runs out of rows in my table.

Author

Commented:
How can we set the max number of times to loop through the foreach statement?  I have eight rows in the table right now, until I get the dynamic add row functionality working.

Author

Commented:
I got it to work by removing the headerrow from the table.  Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial