|
-
Jan 13th, 2006, 12:32 PM
#1
Thread Starter
Hyperactive Member
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:
USE MyDB
INSERT INTO MyTable (Name,Description,Notes)
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
-
Jan 13th, 2006, 12:41 PM
#2
Frenzied Member
Re: SQL and VB.NET 2005
I believe you can use dbnull
VB Code:
USE MyDB
INSERT INTO MyTable (Name,Description,Notes)
If myNotes = "" then
VALUES ('myName','myDescription','DBNull.Value')
Else
VALUES ('myName','myDescription','myNotes')
End If
-
Jan 13th, 2006, 01:36 PM
#3
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
-
Jan 13th, 2006, 01:46 PM
#4
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 01:48 PM
#5
Thread Starter
Hyperactive Member
Re: SQL and VB.NET 2005
This is what I get:
Message "String or binary data would be truncated.
Currently Using: VS 2005 Professional
-
Jan 13th, 2006, 02:12 PM
#6
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
-
Jan 13th, 2006, 02:20 PM
#7
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 02:54 PM
#8
Fanatic Member
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]
-
Jan 13th, 2006, 02:57 PM
#9
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 02:58 PM
#10
Fanatic Member
Re: SQL and VB.NET 2005
That is the way to declare variables in SQL Server. You preface it with an @ symbol.
-
Jan 13th, 2006, 03:12 PM
#11
Thread Starter
Hyperactive Member
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
-
Jan 13th, 2006, 04:04 PM
#12
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
-
Jan 16th, 2006, 08:05 AM
#13
Thread Starter
Hyperactive Member
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
-
Jan 16th, 2006, 08:42 AM
#14
Thread Starter
Hyperactive Member
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
-
Jan 16th, 2006, 08:50 AM
#15
Retired VBF Adm1nistrator
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]
-
Jan 16th, 2006, 09:03 AM
#16
Thread Starter
Hyperactive Member
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
-
Jan 16th, 2006, 09:08 AM
#17
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
-
Jan 16th, 2006, 09:10 AM
#18
Thread Starter
Hyperactive Member
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
-
Jan 16th, 2006, 09:14 AM
#19
Thread Starter
Hyperactive Member
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
-
Jan 16th, 2006, 09:24 AM
#20
Retired VBF Adm1nistrator
Re: SQL and VB.NET 2005
You could also try the ntext data type
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 16th, 2006, 11:59 AM
#21
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
-
Jan 16th, 2006, 07:25 PM
#22
Retired VBF Adm1nistrator
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
-
Jan 16th, 2006, 09:53 PM
#23
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|