Solved

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

Posted on 2014-10-06
19
213 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 33

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

910 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

24 Experts available now in Live!

Get 1:1 Help Now