Reputation: 143
I've read through about 50 sites and stackoverflow questions that advise to do what I have in my code below. I've been through many variations on this, and I can't seem to get it to do anything at all.
I've been watching SQL Profiler and nothing is sent at all. I get no errors.
All I want is a list with checkboxes I can click to change a single setting from 0 to 1. I'm sure it's me, but I'm missing it, and I've spent an embarrassingly long time on this. Thanks for any input.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1" DataKeyNames="npi"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="provcode" HeaderText="provcode"
SortExpression="provcode" />
<asp:BoundField DataField="npi" HeaderText="npi" SortExpression="npi" />
<asp:BoundField DataField="firstname" HeaderText="firstname"
SortExpression="firstname" />
<asp:BoundField DataField="lastname" HeaderText="lastname"
SortExpression="lastname" />
<asp:TemplateField HeaderText="Submit Y/N">
<ItemTemplate>
<asp:CheckBox ID="submitChk" runat="server" Enabled="true" AutoPostBack="True" OnCheckChanged="submit_CheckedChanged"
Checked='<%# Bind("submit") %>'/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<p>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DS_SSRS_ReportsConnectionString1 %>"
SelectCommand="SELECT [provcode], [npi], [firstname], [lastname], [submit]
FROM [DRPprovders]
ORDER BY lastname, firstname"
onselecting="SqlDataSource1_Selecting"
>
</asp:SqlDataSource>
</p>
CodeBehind:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
public partial class Pages_ProvSelect : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataBind();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
protected void submit_CheckedChanged(object sender, EventArgs e)
{
CheckBox submitChk = (CheckBox)sender;
GridViewRow row = (GridViewRow)submitChk.NamingContainer;
//int ID = (int)GridView1.DataKeys[row.DataItemIndex].Value;
String npi = (String)GridView1.DataKeys[row.DataItemIndex].Value;
}
}
Upvotes: 0
Views: 90
Reputation: 11433
You just need to connect to the database and perform the update "manually" in that CheckedChanged method.
Something like this should work:
protected void submit_CheckedChanged(object sender, EventArgs e)
{
CheckBox submitChk = (CheckBox)sender;
GridViewRow row = (GridViewRow)submitChk.NamingContainer;
String npi = (String)GridView1.DataKeys[row.DataItemIndex].Value;
SqlConnection conn = new SqlConnection("your connection string here");
string updateQuery = "UPDATE DRPprovders SET submit = @submit WHERE npi = @npi";
SqlCommand cmd = new SqlCommand(updateQuery, conn);
// If it's checked, pass 1, else pass 0
cmd.Parameters.AddWithValue("@submit", submitChk.Checked ? 1 : 0);
cmd.Parameters.AddWithValue("@npi", npi);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
You might just be able to pass the boolean value of submitChk.Checked (rather than converting to 1's and 0's like I did), I can't remember now (and I was just typing this off the top of my head, so it's untested).
Upvotes: 1