mardi 29 novembre 2016

Update Sql with gridview checkbox

I have a gridview set in ASP.Net that has a name and a checkbox.

          <h4>Current Instructor</h4>
            <div class="hide-overflow">
                <asp:GridView runat="server" ID="GridViewInstructor" DataSourceID="SqlDataSourceInstructor" AutoGenerateColumns="false" CssClass="table table-bordered table-striped" ShowHeader="false" EmptyDataText="No Instructor associated to class." DataKeyNames="Instructor">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="CBInstructorPrimary" runat="server" Checked='<%#Eval("Primary")%>' OnCheckedChanged="PrimaryUpdate" AutoPostBack="true" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="InstructorName" />
                    </Columns>
                </asp:GridView>
                <asp:SqlDataSource runat="server" ID="SqlDataSourceInstructor" DataSourceMode="DataReader"  ConnectionString="<%$ ConnectionStrings:HRAgriConnectionString %>"
                    SelectCommand="
                        SELECT a.Primary, a.ClassID, a.Instructor, b.LName + ', ' + b.FName as InstructorName
                        FROM tblClass a
                        LEFT JOIN tblUser b
                        ON a.Instructor = b.UserName
                        WHERE a.ClassID = @classID
                        ORDER BY a.Primary DESC">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="classID" QueryStringField="cid" Type="String" />
                    </SelectParameters>
                </asp:SqlDataSource>
            </div>  

This makes a gridview table that looks like

enter image description here

my c# code behind looks like this

protected void PrimaryUpdate(object sender, EventArgs e)
{

    //CheckBox activeCheckBox = sender as CheckBox;

    //foreach (GridViewRow rw in GridViewInstructor.Rows)
    //{
    //    CheckBox chkBx = (CheckBox)rw.FindControl("CBInstructorPrimary");
    //    if (chkBx != activeCheckBox)
    //    {
    //        chkBx.Checked = false;
    //    }
    //    else
    //    {
    //        chkBx.Checked = true;
    //    }
    //}

    string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "UPDATE tblClass SET [Primary] = @Primary WHERE classID=@classID and Instructor = @InstructorName";
            cmd.Connection = con;
            con.Open();
            foreach (GridViewRow row in GridViewInstructor.Rows)
            {
                //Get Instructor Name.
                string InstructorName = row.Cells[0].Text;

                //Get the Class Id from the DataKey property.
                classID = Request.QueryString["cid"];

                //Get the checked value of the CheckBox.
                bool Primary = (row.FindControl("CBInstructorPrimary") as CheckBox).Checked;

                //Save to database
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@Name", SalesmanName);
                cmd.Parameters.AddWithValue("@classID", classID);
                cmd.Parameters.AddWithValue("@Primary", Primary);
                cmd.ExecuteNonQuery();
            }
            con.Close();
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    }
}

The outcome should be if you check another checkbox it will update the Primary field (which is a bit field) to 1 if checked and all others in that group will be set to 0. And only 1 checkbox can be checked at a time. I know I have commented out the top part for now. I just haven't been able to figure out how to get this working.




Aucun commentaire:

Enregistrer un commentaire