user3339242
user3339242

Reputation: 641

Change text received from sql query in asp.net

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>

Upvotes: 2

Views: 344

Answers (2)

Moshtaf
Moshtaf

Reputation: 4903

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";
    }
}

Upvotes: 1

JimmyV
JimmyV

Reputation: 540

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

Upvotes: 1

Related Questions