Hi Guys,
I have a checkboxlist and a function to update the checkboxlist which are found inside of an updatepanel. The update button calls a function which deletes values from a table and uses a sqlbulkcopy to map a the boxes chosen to a sql table. I've stepped through the debugger and have noticed that my delete query works, but when I try and map columns from the datatable created in code behind to the sql table, I find that the datatable created in code behind is actually empty. Please help!
My Markup looks like this:
<asp:UpdatePanel runat="server" ID="upnl" UpdateMode="Always" >
<ContentTemplate>
<asp:RadioButtonList runat="server" ID="rbl" RepeatColumns="3" AutoPostBack="true" Width="900px" /><br />
<asp:Panel runat="server" ID="Tree" Visible="false">
<a href="#" id="SelectAll">Select</a> -
<a href="#" id="DeselectAll">Deselect</a>  
<asp:Button runat="server" ID="SaveIt" Text="Save" OnClick="SaveIt_OnClick" /> <asp:Button ID="Drop" runat="server" Text="Toggle" />
<br />
<asp:Panel id="Panel" runat="server" style="display:none;">
<h3><strong><u>Edit</u></strong></h3>
<asp:CheckBoxList ID="Cbl" runat="server" RepeatColumns="3" /><br />
<asp:Button ID="Btn_Submit" Text="Update" runat="server" OnClick="Submit_Click" /><br />
</asp:Panel>
<div id="ItsATree" >
<br />
<ul runat="server" id="TreeLeaf">
</ul>
</div>
<br />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
And the Code Behind function which is supposed to update the values selected in the cbl into the DB looks like this:
protected void Submit_Click(object sender, EventArgs e)
{
String Id = rbl.SelectedValue;
DataTable dt = new DataTable();
dt.Columns.Add("UpdateTID", typeof(String));
dt.Columns.Add("UpFID", typeof(String));
foreach (ListItem li in Cbl.Items)
{
if (li.Selected == true)
{
DataRow dr = dt.NewRow();
dr["UpdateTID"] = Id;
dr["UpdateFID"] = Convert.ToString(li.Value);
dt.Rows.Add(dr);
}
}
using (SqlConnection connection = new SqlConnection(constr))
{
SqlCommand sc = connection.CreateCommand();
sc.CommandText = "DELETE FROM myTable WHERE ID = @id";
sc.Parameters.AddWithValue("@id", Id);
connection.Open();
sc.ExecuteNonQuery();
connection.Close();
connection.Open();
using (SqlBulkCopy sbc = new SqlBulkCopy(connection))
{
sbc.ColumnMappings.Add("UpdateTID", "TID");
sbc.ColumnMappings.Add("UpFID", "FID");
sbc.DestinationTableName = "myTable";
sbc.WriteToServer(dt);
}
connection.Close();
}
}
So when "Btn_Submit" is clicked, its fires off "Submit_Click." When stepping through the code of this function; the delete statement executes but when it comes time to write "sbc" over to the sql table the "dt" table contains no rows.
Thanks for your time and help.