Results 1 to 9 of 9

Thread: [RESOLVED] how to enter null value ?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    198

    Resolved [RESOLVED] how to enter null value ?

    if textbox is not enabled , a null value must be entered in a database.

    i tried this but there is an error " value of type DBnull cannot be converted to string"

    Code:
    If pretTxt.Enabled = False Then pretTxt.Text = DBNull .value

    and i am trying to do this so that i can insert something like this.

    Code:
     If pretTxt.Enabled = False Then pretTxt.Text = DBNull .value
    
            Dim cmd As New SqlCommand("insert into Marks_master (subject_id, student_id, T_PA_marks, T_ET_marks, P_PA_marks,P_ET_marks) VALUES(@si,@stdid, @tet ,@tpa, @pet, @ppa)", con)
            cmd.Parameters.AddWithValue("si", subjectLbl.Text)
            cmd.Parameters.AddWithValue("stdid", studentidTxt.Text)
            cmd.Parameters.AddWithValue("tet", thetTxt.Text)
            cmd.Parameters.AddWithValue("tpa", thpaTxt.Text)
            cmd.Parameters.AddWithValue("pet", pretTxt.Text)
            cmd.Parameters.AddWithValue("ppa", prpaTxt.Text)
    
           Try
                cmd.ExecuteNonQuery()
                MessageBox.Show("Record added")
            Catch ex As Exception
                MessageBox.Show("Error while adding record")
            End Try
            con.Close()

    if textbox is not enabled then null value must be inserted in the table.
    Last edited by hlsc1983; Feb 2nd, 2016 at 10:19 AM.

  2. #2
    Addicted Member omundodogabriel's Avatar
    Join Date
    May 2013
    Posts
    177

    Re: how to enter null value ?

    on vb.net, a null value is Nothing.
    Example:
    Code:
    SomeVar = Nothing
    Althrough im not sure if I understood what youre trying to do. What is DBNull?

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how to enter null value ?

    Quote Originally Posted by hlsc1983 View Post
    if textbox is not enabled , a null value must be entered in a database.

    i tried this but there is an error " value of type DBnull cannot be converted to string"

    Code:
    If pretTxt.Enabled = False Then pretTxt.Text = DBNull .value

    and i am trying to do this so that i can insert something like this.

    Code:
     If pretTxt.Enabled = False Then pretTxt.Text = DBNull .value
    
            Dim cmd As New SqlCommand("insert into Marks_master (subject_id, student_id, T_PA_marks, T_ET_marks, P_PA_marks,P_ET_marks) VALUES(@si,@stdid, @tet ,@tpa, @pet, @ppa)", con)
            cmd.Parameters.AddWithValue("si", subjectLbl.Text)
            cmd.Parameters.AddWithValue("stdid", studentidTxt.Text)
            cmd.Parameters.AddWithValue("tet", thetTxt.Text)
            cmd.Parameters.AddWithValue("tpa", thpaTxt.Text)
            cmd.Parameters.AddWithValue("pet", pretTxt.Text)
            cmd.Parameters.AddWithValue("ppa", prpaTxt.Text)
    
           Try
                cmd.ExecuteNonQuery()
                MessageBox.Show("Record added")
            Catch ex As Exception
                MessageBox.Show("Error while adding record")
            End Try
            con.Close()

    if textbox is not enabled then null value must be inserted in the table.
    Two ways to deal with this... 1) use .Parameters.Add rather than .AddWithValue, but this means creating the parameter, setting its type and size, etc., then setting the value ... or 2) after adding the parameters, before executing the SQL, you update the parameter value.

    Code:
    cmd.Parameters("pet").Value = If(preText.Enabled, pretTxt.Text, DBNull.Value))
    I use a similar approach to ensure that the parameter datatype gets set right, then go back and set NULLs as needed.

    in looking back over that, I guess it's a two step either way.

    Quote Originally Posted by omundodogabriel View Post
    on vb.net, a null value is Nothing.
    Example:
    Code:
    SomeVar = Nothing
    Althrough im not sure if I understood what youre trying to do. What is DBNull?
    Null and Nothing are not the same thing. Nothing means just that, it is nothing... Null on the other hand means unknown. It's used in databases to represent the lack of a value. Some people think it means the same as an empty value, but it's more than that... because in some cases a blank could be a valid value... and distinctly different from a null value. DBNull is .NET's implementation of the NULL concept... I'm not sure if it's a structure or a class, but it is shared, so you can't declare something "as DBNull" but if you want to compare someting from the database to see if it is null or not, you use DBNull.Value.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Re: how to enter null value ?

    I'm not sure if it's a structure or a class
    It's a non inheritable class, with the Value field as a shared member.
    Last edited by kebo; Feb 2nd, 2016 at 11:18 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    198

    Re: how to enter null value ?

    Quote Originally Posted by techgnome View Post
    Two ways to deal with this... 1) use .Parameters.Add rather than .AddWithValue, but this means creating the parameter, setting its type and size, etc., then setting the value ... or 2) after adding the parameters, before executing the SQL, you update the parameter value.

    Code:
    cmd.Parameters("pet").Value = If(preText.Enabled, pretTxt.Text, DBNull.Value))
    I use a similar approach to ensure that the parameter datatype gets set right, then go back and set NULLs as needed.

    in looking back over that, I guess it's a two step either way.



    Null and Nothing are not the same thing. Nothing means just that, it is nothing... Null on the other hand means unknown. It's used in databases to represent the lack of a value. Some people think it means the same as an empty value, but it's more than that... because in some cases a blank could be a valid value... and distinctly different from a null value. DBNull is .NET's implementation of the NULL concept... I'm not sure if it's a structure or a class, but it is shared, so you can't declare something "as DBNull" but if you want to compare someting from the database to see if it is null or not, you use DBNull.Value.

    -tg


    k i am using the this:

    Code:
     
            Dim cmd As New SqlCommand("insert into Marks_master (subject_id, student_id, T_PA_marks, T_ET_marks, P_PA_marks,P_ET_marks) VALUES(@si,@stdid, @tet ,@tpa, @pet, @ppa)", con)
            cmd.Parameters.AddWithValue("si", subjectLbl.Text)
            cmd.Parameters.AddWithValue("stdid", studentidTxt.Text)
            cmd.Parameters.AddWithValue("tet", thetTxt.Text)
            cmd.Parameters.AddWithValue("tpa", thpaTxt.Text)
            cmd.Parameters.AddWithValue("pet", pretTxt.Text)
            cmd.Parameters.AddWithValue("ppa", prpaTxt.Text)
            cmd.Parameters("pet").Value = If(pretTxt.Enabled, pretTxt.Text, DBNull.Value)
    and geeting this error
    Code:
    Cannot infer a common type for the second and third operands of the 'If' operator. One must have a widening conversion to the other.

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how to enter null value ?

    Ungh... ok... well then try this:
    Code:
            cmd.Parameters.AddWithValue("pet", pretTxt.Text)
            cmd.Parameters.AddWithValue("ppa", prpaTxt.Text)
            if not pretTxt.Enabled Then
                cmd.Parameters("pet").Value = DBNull.Value
            End If
    You'd think there was an easier way to deal with this... oh well...

    And I'd swear the pevious example works... I may have to go back and see just exactlky what I did.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    Addicted Member omundodogabriel's Avatar
    Join Date
    May 2013
    Posts
    177

    Re: how to enter null value ?

    Quote Originally Posted by techgnome View Post
    Null and Nothing are not the same thing. Nothing means just that, it is nothing... Null on the other hand means unknown. It's used in databases to represent the lack of a value. Some people think it means the same as an empty value, but it's more than that... because in some cases a blank could be a valid value... and distinctly different from a null value. DBNull is .NET's implementation of the NULL concept... I'm not sure if it's a structure or a class, but it is shared, so you can't declare something "as DBNull" but if you want to compare someting from the database to see if it is null or not, you use DBNull.Value.
    gee, I always thought that the Nothing in vb,net was the equivalent to null in c#.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: how to enter null value ?

    There's null and there's Null (or DBNull) ... in this case, it's taken in the context of Null, from a database - thus the name DBNull ...

    In the C-faimilies null is a representation of char(0), and used for a variety of things - two most notable - releasing pointers (setting a pointer to null effectively clears out the address... this is where it is similar to Nothing in VB) and for terminating strings ala "null-terminated string".

    It's all about the context.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Apr 2013
    Posts
    198

    Re: how to enter null value ?

    It works...you are a life saver.!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width