Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 911
  • Last Modified:

Error with Checkbox in Gridview For Boolean value

I'm trying to put a checkbox into the gridview, and i want it to send a True or a False value into the database. I keep getting an error
Compiler Error Message: CS0103: The name 'True' does not exist in the current context

Source Error:

Line 227:                  <asp:templatefield HeaderText="Send Notification To Tim">
Line 228:                        <EditItemTemplate>
Line 229:                    <asp:CheckBox ID="chkOverrideAllow" runat="server" Checked='<%# (Eval("edit_override") == "True") ? True : False %>'/>
Line 230:                    </EditItemTemplate>
Line 231:                        <ItemTemplate>

I'm not sure what is going wrong

<asp:templatefield HeaderText="Send Notification To Tim">
				<EditItemTemplate>
                    <asp:CheckBox ID="chkOverrideAllow" runat="server" Checked='<%# (Eval("Send_Tim") == "True") ? True : False %>'/>
                    </EditItemTemplate>
				<ItemTemplate>
				<asp:label id="LabelTim" runat="server" Text='<%# Bind("Send_Tim") %>'></asp:label>
                  </ItemTemplate>

Open in new window

0
GravitaZ24
Asked:
GravitaZ24
  • 6
  • 5
  • 2
2 Solutions
 
apeterCommented:
is it lower case "true" you should compare ? DId you checked what exactly the value is coming ?

I think, directly assign the value instead of using tenary operator, it should work fine.
 <asp:CheckBox ID="chkOverrideAllow" runat="server" Checked='<%# (Eval("edit_override")'/>
0
 
GravitaZ24Author Commented:
Hi!
I'm getting "Specified Cast is not Valid" error when i click the update button on Gridview

<asp:templatefield HeaderText="Send Notification To Tim">
                        <EditItemTemplate>
                    <asp:CheckBox ID="chkOverrideAllow" runat="server" Checked='<%#(Eval("Send_Tim")) %>'/>
                </EditItemTemplate>
0
 
apeterCommented:
One way it override RowDataBound Event of Gridview like below and set the checked property in code behind. Else what is the value of <%#(Eval("Send_Tim")) %> ? hope it is "true"

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
  {
    if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Display the company name in italics.
      e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
    }
  }
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
sm394Commented:
<%# Eval("Send").ToString().Equals("True") ? true : false%>
0
 
sm394Commented:
sorry typo in above
this will do
Checked='<%# Eval("Send_Tim").ToString().Equals("True") ? true :false %>'
0
 
GravitaZ24Author Commented:
Thanks again guys, sm394, i tried what you did, I didn't get an error, but nothing was showing up in the database field
0
 
sm394Commented:
A- Do you get checkbox checked or unchecked
B-  what values 'Send_Tim' field has in database table
0
 
GravitaZ24Author Commented:
A- the checkbox is intially unchecked.
B- The fields are all "Null"
0
 
sm394Commented:
kool
with this snipet
Checked='<%# Eval("Send_Tim").ToString().Equals("True") ? true :false %>'
if field val is True checkbox will be checked
if field val is Null or False check box will be unchecked

could you tell us a bit more what you trying to achieve
0
 
GravitaZ24Author Commented:
Eventually what I want to achieve is, my boss asked me to have a field in the database be checked to true or false if a problem is to be sent to a certain person (Tim). When this field is true, i'm going to have a trigger send an e-mail to the person notifying him.
0
 
sm394Commented:
I am not sure for sending email you will use Database mail /trigger or C# email options but you can get
the status of Send_Tim field on RowDataBound event of gridview
try the link below

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx 
 http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/GridViewClassRowDataBound.aspx
0
 
GravitaZ24Author Commented:
Thank you for the help! I was able to figure out he problem was that the null fields already in the table were messing everything up, So i just inserted "False" into every table!
0
 
GravitaZ24Author Commented:
every field i meant
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now