在asp.net c#中我还有一个我不想要的转发器控制线

时间:2014-06-30 05:18:55

标签: c# asp.net sql-server-2008 gridview

我用asp.net和c#显示转发器控件的数据。它的工作正常,但最后又有一条线路正在运行,我不想这样做。 但我无法理解为什么会这样。 这条线是我不想要" System.Data.SqlClient.SqlDataAdapter"

,我的代码是:.aspx

 <asp:GridView ID="GridView1" DataKeyNames="id" AutoGenerateColumns="false" CellPadding="5"
            runat="server">
            <Columns>

                <asp:TemplateField HeaderText="Add To Compare">
                    <ItemTemplate>
                        <asp:CheckBox ID="chkSelect" runat="server" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField HeaderText="companyname" DataField="companyname" />
                <%--   <asp:BoundField HeaderText="sectorname" DataField="sectorname" />--%>
                <asp:BoundField HeaderText="sectorsubname" DataField="sectorsubname" />
                <%--<asp:BoundField HeaderText="Location" DataField="email" />--%>
            </Columns>
            <HeaderStyle BackColor="#df5015" Font-Bold="true" ForeColor="White" />
        </asp:GridView>
        <asp:Button ID="btnProcess" Text="Get Selected Records" runat="server" Font-Bold="true"
            OnClick="btnProcess_Click" /><br />

        <asp:Repeater ID="Repeater3" runat="server">
                            <ItemTemplate>
                                <div class="list-1-item">
                                    <div class="list-1-col">
                                        <h4>
                                            <span><%# Eval("companyname")%></span>
                                        </h4>
                                    </div>
                                    <div class="list-1-col center">
                                        <h3>
                                            <span><%# Eval("fieldname") %></span> <b>&nbsp;:</b>

                                             <span><%# Eval("fielddetails")%></span> <b>&nbsp;</b>
                                        </h3>
                                    </div>
                                    <div class="list-1-col center">
                                        <h3>
                                          <%--  <span><%# Eval("fielddetails")%></span> <b>&nbsp;</b>--%>
                                        </h3>
                                    </div>

                                </div>
                            </ItemTemplate>
                        </asp:Repeater>

,我的代码是:.aspx.cs

 protected void btnProcess_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da;
        DataTable dt = new DataTable();
        //dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Name"), new DataColumn("Country") });
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);
                if (chkRow.Checked)
                {
                    string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
                    con.Open();
                    //string data = "select fieldname,fielddetails from finalcomparedata where compare_id =  " + id.ToString() + "  ";

                    string data = "select cd.companyname as CompanyName,fd.fieldname, fd.fielddetails from finalcomparedata  fd inner join comparedata cd on fd.compare_id = cd.id where compare_id =  " + id.ToString() + "  ";

                    SqlCommand cmd = new SqlCommand(data, con);                    
                    da = new SqlDataAdapter(cmd);                    
                    da.Fill(dt);                    
                    dt.Rows.Add(da);
                } 
                con.Close();
            }
        }
        Repeater3.DataSource = dt;
        Repeater3.DataBind();

    }

1 个答案:

答案 0 :(得分:3)

您要将SqlDataAdapater添加为一行。您的DataTable已填满,无需添加行。

注释掉或删除此行:

dt.Rows.Add(da);

更好的方法

为dataTable定义列,并使用SqlDataReader在数据表中添加新行:

protected void btnProcess_Click(object sender, EventArgs e)
{        
    DataTable dt = new DataTable();
    dt.Columns.Add("companymame");
    dt.Columns.Add("fieldname");
    dt.Columns.Add("fielddetails");

    foreach (GridViewRow row in GridView1.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            CheckBox chkRow = (row.Cells[0].FindControl("chkSelect") as CheckBox);

            if (chkRow.Checked)
            {
                string id = GridView1.DataKeys[row.RowIndex].Value.ToString();
                con.Open();       

                string data = "select cd.companyname as CompanyName,fd.fieldname, fd.fielddetails from finalcomparedata  fd inner join comparedata cd on fd.compare_id = cd.id where compare_id = @id";
                SqlCommand cmd = new SqlCommand(data, con); 
                cmd.Parameters.AddWithValue("@id", id.ToString());                    
                using(SqlDataReader reader = command.ExecuteReader())
                {
                     while (reader.Read())
                     {
                         DataRow dr = dt.NewRow();
                         dr["companyname"] = reader[0].ToString();
                         dr["fieldname"] = reader[1].ToString();
                         dr["fielddetails"] = reader[2].ToString();

                         dt.Rows.Add(dr);               
                      }           
                 }
            } 
            con.Close();
        }
    }

    Repeater3.DataSource = dt;
    Repeater3.DataBind();
}