0

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;

}

}

10
  • your code do not anything! explain more.. Commented Nov 22, 2013 at 18:47
  • Why do you think NamingContainer hold a row of data? Commented Nov 22, 2013 at 18:59
  • @N4TKD, I couldn't say. I was just following several examples I found where others were doing what I need to do. I've never personally used NamingContainer, but that was what others were suggesting so I gave it a shot. Commented Nov 22, 2013 at 19:19
  • @mehdi, There's not a great deal to explain. It's a page with only a grid pulling a small table in it. The column with the checkbox (submit) is set to autopostback and use the submit_CheckedChanged event in the codebehind. That event was written using examples from others. Commented Nov 22, 2013 at 19:21
  • @R_Scott Best step is to debug it walk through the code see what is what I think NamingContainer holds the id of the control just off top of my head. Commented Nov 22, 2013 at 19:24

1 Answer 1

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

That actually may need to be submitChk.Checked ? "1" : "0" Man, I'm rusty on this stuff. I'm too used to using ORMs at this point.
After I found my stupid typo, I used your solution, and that did it. Exactly what I wanted. Thank you for your time. It's greatly appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.