Solved

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

Posted on 2014-10-06
19
226 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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 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…
Suggested Courses
Course of the Month6 days, 17 hours left to enroll

622 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