0

I am stuck with this. I have a gridview and a sqldatasource. I have created a footer row with a linkbutton to add records to MSSQL database.

enter image description here

I can insert all fileds except the last one, the checkbox, that is a BIT. This is my event handler:

protected void lnkInsert_Click(object sender, EventArgs e)
{
    SqlDataSource1.InsertParameters["name"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtName")).Text;

    SqlDataSource1.InsertParameters["username"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtUsername")).Text;

    SqlDataSource1.InsertParameters["pass"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtPass")).Text;

    SqlDataSource1.InsertParameters["uread"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtUread")).Text;

    SqlDataSource1.InsertParameters["udownload"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtUdownload")).Text;

    SqlDataSource1.InsertParameters["udelete"].DefaultValue =
        ((TextBox)GridView1.FooterRow.FindControl("txtUdelete")).Text;

    SqlDataSource1.InsertParameters["umail"].DefaultValue =
       ((TextBox)GridView1.FooterRow.FindControl("txtUmail")).Text;

    SqlDataSource1.InsertParameters["is_admin"].DefaultValue =
        ((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked;

    SqlDataSource1.Insert();

}

I have tried different version of the line about the checkbox, I even tried using Bool, Boolean, etc, but I cannot find the way to make it working.I keep getting errors like "impossible to convert bool in string". How should I write this line:

SqlDataSource1.InsertParameters["is_admin"].DefaultValue =
        ((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked;

Aspx

<asp:TemplateField HeaderText="Admin" SortExpression="is_admin">
<EditItemTemplate>
  <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("is_admin") %>' />
    </EditItemTemplate>
     <FooterTemplate>
      <asp:CheckBox ID="chkCredential" runat="server" />
       </FooterTemplate>
         <ItemTemplate>
          <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Bind("is_admin") %>' Enabled="false" />
         </ItemTemplate>
  </asp:TemplateField>

to make it working? Some help will be appreciated.

10
  • if checkedbox is checked then pass value 1 else 0. Commented Sep 30, 2013 at 5:06
  • @Anand ,what do you mean? How should I do that? Commented Sep 30, 2013 at 5:09
  • if((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked==true) then SqlDataSource1.InsertParameters["is_admin"].DefaultValue=1; else 0 Commented Sep 30, 2013 at 5:10
  • 2
    if((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked==true) then SqlDataSource1.InsertParameters["is_admin"].DefaultValue="1"; else "0" Commented Sep 30, 2013 at 5:24
  • 1
    mark it as answer if it works for you. Commented Sep 30, 2013 at 6:17

2 Answers 2

2

if((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked==true) then SqlDataSource1.InsertParameters["is_admin"].DefaultValue="1"; else "0"

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

Comments

1

Can you try this:

SqlDataSource1.InsertParameters["is_admin"].DefaultValue =
    ((CheckBox)GridView1.FooterRow.FindControl("chkCredential")).Checked.ToString().ToLower();

1 Comment

Thanks. I tried but I get an invalid cast exception here: <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%#....

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.