Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

isDBnull help needed

Options
  • 21-07-2004 3:48pm
    #1
    Registered Users Posts: 937 ✭✭✭


    i have a little function that gets passed a value and a datareader, the function checks for a null value and if it is returns --, but if its not, it should return the variable from sql

    function nullCheck(var as string,objDR as SQLDataReader)
    if isDBnull(var)=0 then
    return "--"
    else
    return objDR(var)
    end if
    end function


    thats the code, but it doesnt seem to work, all it returns is -- the whole time and if i swap the 0 to a 1, .net complains about assigning a null variable

    any help much appreciated


Comments

  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Why are you using IsDBNull? The reason I'm asking is when I first started out with ADO.Net I used IsDBNull and then found out it wasn't necessary. If somebody had pointed this out it would have saved some frustration.

    For the life of me I can't remember what I was doing but give us an outline and we may be able to give you a better way of achieving the task.


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    im pulling info from a sql db, and seperating it out, ie assigning db details to variables, before datalists are suggested, im not unsing them, and some of the db fields are null values, and as .net wont let me assign nulls to variables, i decided to use the above function, hope this makes it a bit clearer


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    its ok, just got it...if isDBnull(objDR(var)) then

    feel so happy now


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Post the code!!! I'd usually use something like this, forgive the C#
    // We'll assume theres a GetDataSet method 
    DataSet ds = GetDataSet("foo"); 
    
    // Get the data row, its one way to skin a cat
    DataRow dr = ds.Tables[0].Rows[0];
    
    // Foo String being the Field name, etc for the next 2 
    string foo_s = (string)dr["Foo String"]; 
    int foo_i = (int)dr["Foo Integer"];
    bool foo_b = (bool)["Foo Boolean"];
    

    my variables foo_s etc will except any null values, I do tend to choose DataSets or DataReaders. Anyway post your code.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Feel so happy now

    :D


  • Advertisement
  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    Originally posted by Evil Phil
    my variables foo_s etc will except any null values,

    Ummm....no they won't. Not safely.

    If you assign null to your foo_i, then the value of foo_i is 0.

    How do you then distinguish between a result which returns a value of 0 and a value of NULL ???

    If the two aren't treated differently, this begs the question of why you'd bother having two seperate values for the same meaning in the database.

    Not only that, but I have a feeling that with strings, a database null could be expressed as either a c# null or an empty string...depending on the DB driver. In the former case, aren't you gonna have to check your string values too, to make sure that they are initialised...and you might as well do that when writing to them from the DB rather than every time you try reading from them in the app itself.

    Personally, I'm more of a fan of ensuring that NULL won't come back from the database and not worrying about it in my code....but there are drawbacks to this approach too...

    jc


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Its a fair cop. You're right bonkey, its not safe. The code that came from won't every return nulls. My bad.


  • Registered Users Posts: 2,781 ✭✭✭amen


    maybe I'm a bit silly but I thought the equality check in
    .Net was == not =
    and why do you need to use it with IsDBNull anway the point of
    any of the Is functions is that they return true/false so you can say something like
    if IsDBNull(var) then
    else
    end if


  • Registered Users Posts: 937 ✭✭✭Diddy Kong


    for vb, its =, i have a lot of items checks to do and would prefer to use a function to save repeating 25odd if else statements


Advertisement