2

I am using a listview in asp.net and inside this listview I have a listbox that gets filled with data using my query. Some of the values that are in the listbox are empty. Is there a way to get the data that is empty and change it to "(Blank)"?

ASP.NET

<asp:ListBox ID="ListBox1" runat="server" DataSourceID="FormTitleDataSource" 
    DataTextField="FormTitle" DataValueField="FormID" 
    SelectedValue='<%# Bind("FormID") %>' AppendDataBoundItems="true">

<asp:SqlDataSource ID="FormTitleDataSource" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" 
    SelectCommand="SELECT DISTINCT FormTitle, FormID FROM Core.Form_Lkup ORDER BY FormTitle">
</asp:SqlDataSource>
3
  • Or does anyone know a way to use the select statement to change the empty strings without changing the value in the table.(using it for an application) Commented Aug 27, 2014 at 18:27
  • In Oracle, you could do select NVL(FormTitle, '(Blank)'). Not sure if there's an equivalent syntax for SQL Server (NVL stands for No Value, it's a function that replaces null with a specified value.) Commented Aug 27, 2014 at 18:35
  • post up your sql so we can offer a sql based solution Commented Aug 27, 2014 at 18:56

2 Answers 2

1

Have you tried writing it into your SQL statement?

SELECT DISTINCT case when FormTitle = '' then '(Blank)' else FormTitle end as FormTitle, FormID FROM Core.Form_Lkup ORDER BY FormTitle
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot to end my case statement...fixed
1

Add OnDataBound="ListBox1_DataBound" to your ListBox control:

<asp:ListBox ID="ListBox1" runat="server" OnDataBound="ListBox1_DataBound" DataSourceID="FormTitleDataSource" 
    DataTextField="FormTitle" DataValueField="FormID" 
    SelectedValue='<%# Bind("FormID") %>' AppendDataBoundItems="true">

Then add this method in codebehind:

protected void ListBox1_DataBound(object sender, EventArgs e)
{
    foreach (ListItem i in ListBox1.Items)
    {
        if (i.Value == "")
            i.Text = "Blank";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.