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

x
?
Solved

How do I replace null radiobuttonlist value with "" in my sql insert statement?

Posted on 2014-10-06
19
Medium Priority
?
231 Views
Last Modified: 2014-10-27
this is my HTML for my radiobuttonlist control:

 
                                            <asp:RadioButtonList ID="ddlEmpORCons" runat="server" RepeatDirection="Horizontal">
                                                <asp:ListItem Text="Employee" Value="0" />
                                                <asp:ListItem Text="Consultant" Value="1" />
                                                <asp:ListItem Text="Temp" Value="2" />
                                            </asp:RadioButtonList>

Open in new window


as you can see there is no default value (list item) selected (checked), this is by design. when i'm saving the values of my page's controls, using and SQL insert statement:

                sqlStr = "INSERT INTO ais2000d.ACCESS (AID, ATYPE, AREQDATE, AEMPNAME, AHRTRMDATE, ASUP, ABRANCH, AEMPTYPE, " +
                    "ACOMMENT, ACOMPNAME, APHONEXT, AMODAFTR, ANEEDEMAL) VALUES (\'" + intSequence + "\', \'" + 
                    ddlRequestType1.SelectedItem.Text.ToString().Trim() + "\', \'" + iDtOfReq + "\', \'" + txtEmpName.Text.Trim() + 
                    "\', \'" + iDtOfHrOrTrm + "\', \'" + txtImmSupName.Text.Trim() + "\', \'" + txtBranch.Text.ToString().Trim() + 
                    "\', \'" + (ddlEmpORCons.SelectedItem.Text.ToString().Trim() ?? string.Empty) + "\', \'" + txtComment.Text.Trim().Replace("'", "‘") + 
                    "\', \'" + txtCompName.Text.Trim() + "\', \'" + txtPrevExt.Text.Trim() + "\', \'" + 
                    txtModelAfterTextBox.Text.Trim() + "\'" + (ddlNeedEmail.SelectedItem.Text.ToString().Trim() ?? string.Empty) + "\')";

Open in new window


i want to be able to check for if the radiobuttonlist control is null, (no list item selected (checked), and if so, place an empty string in place for that value for my insert statement (inline), as i'm attempting to do here. in my SQL statement above, i'm still getting a null error exception the way I have it written, is there a way to do this so that I get the empty string like I want?
0
Comment
Question by:Michael Sterling
  • 9
  • 5
  • 4
  • +1
19 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40364132
use the ISNULL function in SQL

should be something like ...
ISNULL(ddlEmpORCons.SelectedItem.Text.ToString().Trim(), string.Empty)
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40364137
sqlStr = "INSERT INTO ais2000d.ACCESS (AID, ATYPE, AREQDATE, AEMPNAME, AHRTRMDATE, ASUP, ABRANCH, AEMPTYPE, " +
                    "ACOMMENT, ACOMPNAME, APHONEXT, AMODAFTR, ANEEDEMAL) VALUES (\'" + intSequence + "\', \'" + 
                    ddlRequestType1.SelectedItem.Text.ToString().Trim() + "\', \'" + iDtOfReq + "\', \'" + txtEmpName.Text.Trim() + 
                    "\', \'" + iDtOfHrOrTrm + "\', \'" + txtImmSupName.Text.Trim() + "\', \'" + txtBranch.Text.ToString().Trim() + 
                    "\', \'" + ISNULL((ddlEmpORCons.SelectedItem.Text.ToString().Trim(),string.Empty))+ "\', \'" + txtComment.Text.Trim().Replace("'", "‘") + 
                    "\', \'" + txtCompName.Text.Trim() + "\', \'" + txtPrevExt.Text.Trim() + "\', \'" + 
                    txtModelAfterTextBox.Text.Trim() + "\'" + ISNULL((ddlNeedEmail.SelectedItem.Text.ToString().Trim(), string.Empty)) + "\')";

Open in new window


I put it in for the email as well
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40364141
You cannot access the Text property if SelectedItem is null. You must check if SelectedItem is null:
ddlEmpORCons.SelectedItem == null ? ddlEmpORCons.SelectedItem.Text.ToString().Trim() ?? string.Empty

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364143
@plusone3055: VS is telling me that "ISNULL does not exist in this current context"?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364158
@louis: in my SQL statement, that line gets me: "Operator ?? cannot be applied to operands of type 'string' and 'bool'"
outside my SQL statement I get: "Only assign, call, increment, decrement and new object expressions can be used as a statement."

                ddlEmpORCons.SelectedItem == null ? ddlEmpORCons.SelectedItem.Text.ToString().Trim() ?? string.Empty;
                sqlStr = "INSERT INTO ais2000d.ACCESS (AID, ATYPE, AREQDATE, AEMPNAME, AHRTRMDATE, ASUP, ABRANCH, AEMPTYPE, " +
                    "ACOMMENT, ACOMPNAME, APHONEXT, AMODAFTR, ANEEDEMAL) VALUES (\'" + intSequence + "\', \'" + 
                    ddlRequestType1.SelectedItem.Text.ToString().Trim() + "\', \'" + iDtOfReq + "\', \'" + txtEmpName.Text.Trim() + 
                    "\', \'" + iDtOfHrOrTrm + "\', \'" + txtImmSupName.Text.Trim() + "\', \'" + txtBranch.Text.ToString().Trim() + 
                    "\', \'" + ddlEmpORCons.SelectedItem == null ? ddlEmpORCons.SelectedItem.Text.ToString().Trim() ?? string.Empty + "\', \'" + txtComment.Text.Trim().Replace("'", "‘") + 
                    "\', \'" + txtCompName.Text.Trim() + "\', \'" + txtPrevExt.Text.Trim() + "\', \'" + 
                    txtModelAfterTextBox.Text.Trim() + "\'" + 
                    ddlNeedEmail.SelectedItem == null ? ddlNeedEmail.SelectedItem.Text.ToString().Trim() ?? string.Empty + "\')";

Open in new window

0
 
LVL 11

Expert Comment

by:louisfr
ID: 40364176
A mistake, it should have been : instead of ??.
Also, the  concatenation operator has a higher priority than the conditional operator. Put parentheses around the sub-expression
... + (ddlEmpORCons.SelectedItem == null ? ddlEmpORCons.SelectedItem.Text.ToString().Trim() : string.Empty) + ...

Open in new window

0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40364188
the other way I know how to do it is to create your own function....

Public Function IsThisNull(value As String, defaultValue As String) as String
    If String.IsNullOrEmpty(value) Then
        Return defaultValue
    Else
        Return value
    End If
End Function

Open in new window


Then do this in your code
IsThisNull((ddlEmpORCons.SelectedItem.Text.ToString().Trim(),string.Empty))

Open in new window

0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364194
@louis: I'm still getting my original null reference error, so unless there's another way, i think i'm going to have to check for null outside of the SQL, and use a local variable to hold the value(s) of my radiobuttonlist controls. :-(. I'm still open to suggestions.
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 40364200
Mike,

did you try the function I just posted
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364219
@plusone3055: still hitting that null message so i think i'm gonna have to do it before the SQL statment. :-(. oh well, it is what it is.
0
 
LVL 11

Expert Comment

by:louisfr
ID: 40364221
What exactly is null?
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364224
@plusone3055: here's the C# version. I tried it both in and outside of the SQL statement and still got the null error each time. never even made it inside the function.


    protected string IsThisNull(string value, string defaultVal)
    {
        if (value.Equals(null))
            return defaultVal;
        else
            return defaultVal;
    }

Open in new window

0
 
LVL 11

Expert Comment

by:louisfr
ID: 40364232
Is ddlEmpORCons null?
Or the SelectedItem?
Or the Text?
That's what you have to check.

Something like
thing_that_can_be_null ? use_the_thing_when_not_null : string.Empty

Open in new window

0
 
LVL 35

Expert Comment

by:sarabande
ID: 40364234
a radio button group which doesn't has an initial default is violating common style guidelines. the problem: if the user once made a selection, it is no more possible to unselect (beside you do it programmatically somehow what would it make worse).

why not add a button 'Other' to the group and make it the default?

Sara
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364245
@louisfr: the selectedItem is null. when i put that code back into my SQL i get a red "squiggly" line under ddlEmpOrCons.SelectedItem. and when i hover over it it tells me that: "Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'bool'"
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364249
@louisfr:

(ddlEmpORCons.SelectedItem ? ddlEmpORCons.SelectedItem.Text.ToString() : string.Empty)
0
 
LVL 1

Author Comment

by:Michael Sterling
ID: 40364256
@sarabande: i'd love to, the requirement maker of this project is very, "inflexible". :-(
0
 
LVL 11

Accepted Solution

by:
louisfr earned 2000 total points
ID: 40365313
That code:
(ddlEmpORCons.SelectedItem == null ? ddlEmpORCons.SelectedItem.Text.ToString().Trim() : string.Empty)

Open in new window

should not get you the error unless either:
- ddlEmpORCons is null
or
- ddlEmpORCons.SelectedItem is not null but ddlEmpORCons.SelectedItem.Text is.
0
 
LVL 1

Author Closing Comment

by:Michael Sterling
ID: 40407496
Thank you.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question