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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

isDBnull help needed

  • 21-07-2004 2:48pm
    #1
    Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 937 ✭✭✭Diddy Kong


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

    feel so happy now


  • Registered Users, Registered Users 2 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, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    Feel so happy now

    :D


  • Advertisement
  • Registered Users, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 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