Solved

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

Posted on 2014-10-06
19
211 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:mikesExpertExchange
  • 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
 
LVL 1

Author Comment

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

Author Comment

by:mikesExpertExchange
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:mikesExpertExchange
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 1

Author Comment

by:mikesExpertExchange
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:mikesExpertExchange
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 32

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:mikesExpertExchange
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:mikesExpertExchange
ID: 40364249
@louisfr:

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

Author Comment

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

Accepted Solution

by:
louisfr earned 500 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:mikesExpertExchange
ID: 40407496
Thank you.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

705 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now