Link to home
Start Free TrialLog in
Avatar of charlestonlaw
charlestonlaw

asked on

Insert &nbsp or " " instead of DbNull when field left blank in gridview?

I have a Gridview that does updating and when I leave a field blank it inserts a null value into the database instead of "".  In my case the airline name is optional so if the user leaves it blank i want it to insert "" instead of sql server showing <i>null</i>.

I've been playing around but this is only as far as I have gotten... I think I'm heading in the right direction, any ideas?

protected void grdAircraft_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        foreach (DictionaryEntry entry in e.NewValues)
        {
            //if (e.NewValues[entry.Value] == null) { e.NewValues[entry.Value] = ""; }
        }
    }

----- OR -----

Could I change my SQL query so it could work with nulls?

SELECT aircraftID, aircraftManu + ' ' + aircraftName + ' ' + aircraftNumber AS aName FROM aircraft ORDER BY aName
I'm binding a dropdownlist to this query but when aircraftName(which is the field I don't want null) is null, all rows with that null value don't show up in my results.  Could I change the SQL query to have it show up or do I need go attack it with the method above so I can get this to work?

Thanks
Avatar of Reza Rad
Reza Rad
Flag of New Zealand image

use this query istead of your query:
------
SELECT aircraftID, aircraftManu + ' ' + aircraftName + ' ' + aircraftNumber AS aName FROM aircraft ORDER BY aName
where aircraftName is not null
union
SELECT aircraftID, aircraftManu + '   ' + aircraftNumber AS aName FROM aircraft ORDER BY aName
where artifactName is null

------
ASKER CERTIFIED SOLUTION
Avatar of Chirag1211
Chirag1211
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of charlestonlaw
charlestonlaw

ASKER

Is there any way to use the + in SQL Server 2005? I'm getting an error with them.
This is the error:
Error in list of function arguments: '+' not recognized.
Unable to parse query text.
Okay it's late... I know the + works just wondering why your query is giving me an error Chirag...

Thanks!
SELECT aircraftID, aircraftManu + ' ' + ISNULL(aircraftName, ' ') + ' ' + aircraftNumber AS aName FROM aircraft ORDER BY aName

the code above works..

Thanks!
it is CHIRAG query:

SELECT 'aircraftID', 'aircraftManu' + IsNUll('aircraftName',' ') as aircraftName , 'aircraftNumber' AS aName --FROM aircraft ORDER BY aName

must replaced + with ,
probebly chirag right it fast if not actually he found this