Results 1 to 23 of 23

Thread: SQL and VB.NET 2005

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    SQL and VB.NET 2005

    How would I insert values using a command string where one of the values is null but the field in the DB will accept null?

    For example:

    VB Code:
    1. USE MyDB
    2. INSERT INTO MyTable (Name,Description,Notes)
    3. VALUES ('myName','myDescription',' ')

    The last Value is null but I keep getting a truncated error when executing the command. Now keep in mind, not all of my notes will be null values.


    Thanks in advance!!
    Currently Using: VS 2005 Professional

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: SQL and VB.NET 2005

    I believe you can use dbnull
    VB Code:
    1. USE MyDB
    2. INSERT INTO MyTable (Name,Description,Notes)
    3. If myNotes = "" then
    4.    VALUES ('myName','myDescription','DBNull.Value')
    5. Else
    6.    VALUES ('myName','myDescription','myNotes')
    7. End If

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

    Re: SQL and VB.NET 2005

    SQL Server isn't going to know what DBNull.Value means.... That's a .NET thing....

    If you want to insert a NULL ... use the NULL keyword....
    It would help to know if you want the solution in VB, or in SQL... you've mixed your example, and the one above makes the problem worse.

    -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

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    Sorry about that. I am assigning the command to a string and using the SQLCommand.ExecuteNonQuery method to work the database on this issue. I know it is not the most elegant way to do it but I am the type that needs to know how something works first before I can excel at it.

    Would I use NULL from VB or is that an SQL keyword?
    Currently Using: VS 2005 Professional

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    This is what I get:

    Message "String or binary data would be truncated.
    Currently Using: VS 2005 Professional

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

    Re: SQL and VB.NET 2005

    Let's try this; post the code that creates the SQL string, and the code that is executing it.

    -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

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    [Highlight=VB]

    Dim CommandString As String
    Dim SQLCmd as SQLClient.SQLCommand

    CommandString = "USE MyDB " & _
    " INSERT INTO myTable " & _
    " ( Name, Description, Notes ) " & _
    " VALUES ('Jerry','A Kewl D00d',' ')"

    SQLCmd = New SqlClient.SqlCommand(CommandString, mySQLConnection)
    SQLCmd.ExecuteNonQuery()


    [\VBCODE]
    Currently Using: VS 2005 Professional

  8. #8
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: SQL and VB.NET 2005

    This is how I do it.

    Note: You may need to create a function for determining when to pass Null to the DB. I have a function call EmptyToNull that checks the parameter passed in to see if it is an empty string and returns back DBNull.Value.)

    [Highlight=VB]
    Dim CommandString As String
    Dim SQLCmd as SQLClient.SQLCommand

    CommandString = "USE MyDB " & _
    " INSERT INTO myTable " & _
    " ( Name, Description, Notes ) " & _
    " VALUES (?,?,?)"

    SQLCmd = New SqlClient.SqlCommand(CommandString, mySQLConnection)
    SQLCmd.Parameters.Add(New SqlParameter("@Name", SqlDbType.VarChar)).Value = "Jerry"
    SQLCmd.Parameters.Add(New SqlParameter("@Description", SqlDbType.VarChar)).Value = "A Kewl D00d"
    SQLCmd.Parameters.Add(New SqlParameter("@Notes", SqlDbType.VarChar)).Value = DBNull.Value
    SQLCmd.ExecuteNonQuery()
    [\VBCODE]

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    Ok, n00b13 here, what does the "@" mean in front of the Name/Description/Notes variable names?
    Currently Using: VS 2005 Professional

  10. #10
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: SQL and VB.NET 2005

    That is the way to declare variables in SQL Server. You preface it with an @ symbol.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    I keep getting errors on the ?'s. Am I supposed to leave those blank?
    Currently Using: VS 2005 Professional

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

    Re: SQL and VB.NET 2005

    You're doing too much in your command string..... Dump the USe myDB part... you don't need that. The database will be specified based on the connectionString in the connection.

    -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??? *

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    I am still getting the "Incorrect Syntax near ?" error, even without the USE command.

    Anymore thoughts?
    Currently Using: VS 2005 Professional

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    Ok, I figured out to put the variable names in the VALUES statement and now that part works. I am still back to the same problem as before where it tells me that the string or binary data will be truncated. What does this mean and how can I get past it? Will I need to just make a lengthy statement of if...then in order to insert data dynamically accorrding to non-null values?

    Thanks again for all the help!
    Currently Using: VS 2005 Professional

  15. #15
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: SQL and VB.NET 2005

    If you want to insert a null value, then just don't specify it as one of the parameters:
    Code:
    USE MyDB
    INSERT INTO MyTable (Name,Description)
    VALUES ('myName','myDescription')
    That will insert the values into Name and Description, and nulls into all of the other fields in the table.

    In relation to the truncation warning, how is the table setup. What is the data type for each field that you have? I'm guessing you have set some very short field length or something?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    All of my fields are set at varChar. I thought this would make sure to shorten or lengthen the field according to the stored values. The above code will work but I need to have it enter the myNotes into the Notes field of the same table when myNotes are present.

    For example, I have 3 people, John, Joe, and Mary. They all have a name and description but only Joe has notes. How would I be able to input all 3 in the same statement without picking out myNotes in a seperate case statement?

    I hope I explained this ok.

    Thanks
    Currently Using: VS 2005 Professional

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

    Re: SQL and VB.NET 2005

    While varchars will store only the text you send it (as opposed to char fields which pad with spaces)... varchar fields do have a limit. Depending on what you set the length to when defining the table structure, you might be exceeding the length. This will generate the error you are seeing.

    -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??? *

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    What data type should I be using? I am not sending strings of more then 25 characters at the moment. At any given time, my strings will be no more then 50 characters or so. The maximum an SQL field will take is 255 if I remember correctly and there is no way I can exceed that with my current values.
    Currently Using: VS 2005 Professional

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    Re: SQL and VB.NET 2005

    Well, I take that back. It looks like my CREATE statement of this table turned it into a varChar(1) datatype. I am guessing I need to expand that to 255 when I declare them huh? Very sorry for the confusions, I am trying to get up to speed on SQL Server.

    Thanks to ALL who helped me out!!!
    Currently Using: VS 2005 Professional

  20. #20
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: SQL and VB.NET 2005

    You could also try the ntext data type
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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

    Re: SQL and VB.NET 2005

    BTW: You can have more than 255 for your varchar fields... We've got some that are 4k... depends on what you want to put into it. The make byte size for any given row in SQL server is 8k.

    -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??? *

  22. #22
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Re: SQL and VB.NET 2005

    What about ntext?
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

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

    Re: SQL and VB.NET 2005

    the only advantage ntext has is the ability to store unicode text. Since unicode takes two bytes per char, it actualy cuts the capacity in half, since it still cannot exceed 8k per row.

    -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??? *

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