I have a web page (intranet) with four checkboxes pulling from bit fields in a database, all with AutoPostBack = True. To make this work, I have four functions to update the appropriate filed when the box is either checked or unchecked. It seems to me, however, that I should be able to use one function since they are all the same except for the field name to be updated
and then the aspx
If I change the ID to match the field name, can I use that to specify which field to update and therefore have only one function to update the checkbox fields? And if so, how would I go about doing that?
Thanks,
Willie
Code:
protected void chkSelect_CheckedChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
HiddenField hiddenID = (HiddenField)row.FindControl("TranID");
string sql = "UPDATE EcompKviews.dbo.VisaTransactionImportII SET NoReceiptNeeded=@Status WHERE TransactionID=@ID";
string connStrII = ConfigurationManager.ConnectionStrings["visa"].ConnectionString;
using (var con = new SqlConnection(connStrII))
using (var updateCommand = new SqlCommand(sql, con))
{
updateCommand.Parameters.AddWithValue("@ID", int.Parse(hiddenID.Value));
updateCommand.Parameters.AddWithValue("@Status", chk.Checked);
con.Open();
int updated = updateCommand.ExecuteNonQuery();
}
}
protected void chkSelect_CheckedChanged2(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
HiddenField hiddenID = (HiddenField)row.FindControl("TranID");
string sql = "UPDATE EcompKviews.dbo.VisaTransactionImportII SET TaxCollected=@Status WHERE TransactionID=@ID";
string connStrII = ConfigurationManager.ConnectionStrings["visa"].ConnectionString;
using (var con = new SqlConnection(connStrII))
using (var updateCommand = new SqlCommand(sql, con))
{
updateCommand.Parameters.AddWithValue("@ID", int.Parse(hiddenID.Value));
updateCommand.Parameters.AddWithValue("@Status", chk.Checked);
con.Open();
int updated = updateCommand.ExecuteNonQuery();
}
}
protected void chkSelect_CheckedChanged3(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
HiddenField hiddenID = (HiddenField)row.FindControl("TranID");
string sql = "UPDATE EcompKviews.dbo.VisaTransactionImportII SET ReceiptReceived=@Status WHERE TransactionID=@ID";
string connStrII = ConfigurationManager.ConnectionStrings["visa"].ConnectionString;
using (var con = new SqlConnection(connStrII))
using (var updateCommand = new SqlCommand(sql, con))
{
updateCommand.Parameters.AddWithValue("@ID", int.Parse(hiddenID.Value));
updateCommand.Parameters.AddWithValue("@Status", chk.Checked);
con.Open();
int updated = updateCommand.ExecuteNonQuery();
}
}
protected void chkSelect_CheckedChanged4(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
GridViewRow row = (GridViewRow)chk.NamingContainer;
HiddenField hiddenID = (HiddenField)row.FindControl("TranID");
string sql = "UPDATE EcompKviews.dbo.VisaTransactionImportII SET DoS=@Status WHERE TransactionID=@ID";
string connStrII = ConfigurationManager.ConnectionStrings["visa"].ConnectionString;
using (var con = new SqlConnection(connStrII))
using (var updateCommand = new SqlCommand(sql, con))
{
updateCommand.Parameters.AddWithValue("@ID", int.Parse(hiddenID.Value));
updateCommand.Parameters.AddWithValue("@Status", chk.Checked);
con.Open();
int updated = updateCommand.ExecuteNonQuery();
}
}
and then the aspx
Code:
<asp:TemplateField HeaderText="No Receipt Needed" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblNoReceiptNeeded" runat="server" Text='<%#Eval("NoReceiptNeeded") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChckBxNoReceiptNeeded" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged"></asp:CheckBox>
</EditItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tax Collected" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblTaxCollected" runat="server" Text='<%#Eval("TaxCollected") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChckBxTaxCollected" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged2"></asp:CheckBox>
</EditItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="Receipt Received" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblReceiptReceived" runat="server" Text='<%#Eval("ReceiptReceived") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChckBxReceiptReceived" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged3"></asp:CheckBox>
</EditItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderText="DoS" HeaderStyle-Width="10%">
<ItemTemplate>
<asp:Label ID="lblDoS" runat="server" Text='<%#Eval("DoS") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBox ID="ChckBxDoS" runat="server" AutoPostBack="true" OnCheckedChanged="chkSelect_CheckedChanged4"></asp:CheckBox>
</EditItemTemplate>
<HeaderStyle Width="10%"></HeaderStyle>
</asp:TemplateField>
If I change the ID to match the field name, can I use that to specify which field to update and therefore have only one function to update the checkbox fields? And if so, how would I go about doing that?
Thanks,
Willie