Solved

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

Posted on 2014-10-06
19
218 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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
 
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 34

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
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…

726 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