Answered by:
If Not String.IsNullOrEmpty

Question
-
User-1197887286 posted
I have a WebPage with VB.NET - I have an inventory system that if a part number is not showing in a textbox (example textbox45.text) then it skips the command
and goes to the next one (textbox51.text). I have tried a couple of commands
'If Not String.IsNullOrEmpty(TextBox45.Text) Then
or
'If TextBox45.Text = True thenIt returns an error @TransactionName - However, when I check the SQL data table, it did add only the data with text
in the textbox(s) and skipped the blank one(s) as I wanted to do. But can't get past the error.Any help would be great! thanks.
cmd.Parameters.Clear()
'If Not String.IsNullOrEmpty(TextBox45.Text) Then
'If TextBox45.Text = True then
cmd.CommandType = CommandType.Text
With cmd.Parameters
.AddWithValue("@TransactionName", "Consumed")
.AddWithValue("@TransactionDate", TextBox2.Text)
.AddWithValue("@PurchaseOrder", TextBox8.Text)
.AddWithValue("@UnitsSold", "-1")
.AddWithValue("@Date", TextBox2.Text)
.AddWithValue("@StationID", TextBox1.Text)
.AddWithValue("@WhseID", TextBox4.Text)
.AddWithValue("@WipLocation", DropDownList1.Text)
.AddWithValue("@InvName", TextBox45.Text)
End With
'End Ifcmd.Connection = cn
cn.Open()cmd.ExecuteNonQuery()
cn.Close()
cmd.Parameters.Clear()
'If Not String.IsNullOrEmpty(TextBox51.Text) Then
'If TextBox51.Text = True Then
cmd.CommandType = CommandType.Text
With cmd.Parameters
.AddWithValue("@TransactionName", "Consumed")
.AddWithValue("@TransactionDate", TextBox2.Text)
.AddWithValue("@PurchaseOrder", TextBox8.Text)
.AddWithValue("@UnitsSold", "-1")
.AddWithValue("@Date", TextBox2.Text)
.AddWithValue("@StationID", TextBox1.Text)
.AddWithValue("@WhseID", TextBox4.Text)
.AddWithValue("@WipLocation", DropDownList1.Text)
.AddWithValue("@InvName", TextBox51.Text)
End With
'End Ifcmd.Connection = cn
cn.Open()cmd.ExecuteNonQuery()
cn.Close()
Monday, April 14, 2014 10:30 AM
Answers
-
User281315223 posted
The use of the String.IsNullOrEmpty() method is the correct approach to check if a string is empty or not :
'Use a connection to encapsulate all of your calls ' Using cn = New SqlConnection("Your Connection String") 'Build your specific query that is reused in each of these here' Dim query = "INSERT INTO YourTable VALUES(@TransactionName,@TransactionDate,@PurchaseOrder,@UnitsSold,@Date,@StationID,@WhseID,@WipLocation,@InvName)" 'Open your connection' cn.Open() 'If your TextBox45 TextBox is not empty, use it' If Not String.IsNullOrEmpty(TextBox45.Text) Then 'Build a new command to execute here' Using cmd = New SqlCommand(query, cn) 'Populate your parameters ' With cmd.Parameters .AddWithValue("@TransactionName", "Consumed") .AddWithValue("@TransactionDate", TextBox2.Text) .AddWithValue("@PurchaseOrder", TextBox8.Text) .AddWithValue("@UnitsSold", "-1") .AddWithValue("@Date", TextBox2.Text) .AddWithValue("@StationID", TextBox1.Text) .AddWithValue("@WhseID", TextBox4.Text) .AddWithValue("@WipLocation", DropDownList1.Text) .AddWithValue("@InvName", TextBox45.Text) End With 'Execute this query' cmd.ExecuteNonQuery() End Using End If 'If your TextBox51 TextBox is not empty, use it' If Not String.IsNullOrEmpty(TextBox51.Text) Then 'Build a new command to execute here' Using cmd = New SqlCommand(query, cn) 'Populate your parameters ' With cmd.Parameters .AddWithValue("@TransactionName", "Consumed") .AddWithValue("@TransactionDate", TextBox2.Text) .AddWithValue("@PurchaseOrder", TextBox8.Text) .AddWithValue("@UnitsSold", "-1") .AddWithValue("@Date", TextBox2.Text) .AddWithValue("@StationID", TextBox1.Text) .AddWithValue("@WhseID", TextBox4.Text) .AddWithValue("@WipLocation", DropDownList1.Text) .AddWithValue("@InvName", TextBox51.Text) End With 'Execute this query' cmd.ExecuteNonQuery() End Using End If End Using
Additionally, when you add your parameters, you may want to ensure that the types that you are using match those that your Database is expecting, for example if your UnitSold field is expecting an integer, you may want to pass an actual -1 integer to it as opposed to a string version of -1 ("-1") and the same with your Date values :
.AddWithValue("@UnitsSold", -1) .AddWithValue("@Date", DateTime.Parse(TextBox2.Text))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 14, 2014 10:51 AM
All replies
-
User281315223 posted
The use of the String.IsNullOrEmpty() method is the correct approach to check if a string is empty or not :
'Use a connection to encapsulate all of your calls ' Using cn = New SqlConnection("Your Connection String") 'Build your specific query that is reused in each of these here' Dim query = "INSERT INTO YourTable VALUES(@TransactionName,@TransactionDate,@PurchaseOrder,@UnitsSold,@Date,@StationID,@WhseID,@WipLocation,@InvName)" 'Open your connection' cn.Open() 'If your TextBox45 TextBox is not empty, use it' If Not String.IsNullOrEmpty(TextBox45.Text) Then 'Build a new command to execute here' Using cmd = New SqlCommand(query, cn) 'Populate your parameters ' With cmd.Parameters .AddWithValue("@TransactionName", "Consumed") .AddWithValue("@TransactionDate", TextBox2.Text) .AddWithValue("@PurchaseOrder", TextBox8.Text) .AddWithValue("@UnitsSold", "-1") .AddWithValue("@Date", TextBox2.Text) .AddWithValue("@StationID", TextBox1.Text) .AddWithValue("@WhseID", TextBox4.Text) .AddWithValue("@WipLocation", DropDownList1.Text) .AddWithValue("@InvName", TextBox45.Text) End With 'Execute this query' cmd.ExecuteNonQuery() End Using End If 'If your TextBox51 TextBox is not empty, use it' If Not String.IsNullOrEmpty(TextBox51.Text) Then 'Build a new command to execute here' Using cmd = New SqlCommand(query, cn) 'Populate your parameters ' With cmd.Parameters .AddWithValue("@TransactionName", "Consumed") .AddWithValue("@TransactionDate", TextBox2.Text) .AddWithValue("@PurchaseOrder", TextBox8.Text) .AddWithValue("@UnitsSold", "-1") .AddWithValue("@Date", TextBox2.Text) .AddWithValue("@StationID", TextBox1.Text) .AddWithValue("@WhseID", TextBox4.Text) .AddWithValue("@WipLocation", DropDownList1.Text) .AddWithValue("@InvName", TextBox51.Text) End With 'Execute this query' cmd.ExecuteNonQuery() End Using End If End Using
Additionally, when you add your parameters, you may want to ensure that the types that you are using match those that your Database is expecting, for example if your UnitSold field is expecting an integer, you may want to pass an actual -1 integer to it as opposed to a string version of -1 ("-1") and the same with your Date values :
.AddWithValue("@UnitsSold", -1) .AddWithValue("@Date", DateTime.Parse(TextBox2.Text))
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, April 14, 2014 10:51 AM -
User-1197887286 posted
Thank you! will try this and post back the result.
Monday, April 14, 2014 11:28 AM