הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, January 27, 2010 6:41 PM
Can anyone tell me how to add a comma to two values?
What I'm trying to do is read two values into variables, combine them with a comma in the middle, create an array and store the result in the array; e.g. var1,var2
It doesn't have to be a comma, a space will do. Just something I can later use to split the array when I read it.
Do While rdr2.Read()
Dim var1 As String = rdr2.Item("Week").ToString
Dim var2 As String = CStr(rdr2.Item("L1").ToString)
Dim var3 As String = CStr(rdr2.Item("L2").ToString)
Dim LSArraytemp As String
i += 1
LSArray(i) = String.Concat(var2, strComma, var3)
LSArraytemp = LSArray(i)
Loop
What happens is the variables are treated as Integers if I try putting anything between them, so adding a comma doesn't work at all and adding another symbol such as "-" results in an arithmetic result (i.e. LSArray(i) = result of var2 - var3).
If I leave out the comma or other symbol it combines exactly as I'd expect - i.e. the result = var2var3
Edit: I should perhaps give an example to show what I am trying to do:
var2 = 3
var3 = 5
LSArray(i) = 3**,** 5
All replies (15)
Wednesday, January 27, 2010 10:34 PM ✅Answered
Ah. I've got it! Funny how you can stare at something for hours and hours and still not see where you are going wrong, huh?
The String had to be put within single quotes.
Here's the code:
Do While rdr2.Read()
Dim var1 As String = rdr2.Item("Week").ToString
Dim var2 As String = rdr2.Item("L1").ToString
Dim var3 As String = rdr2.Item("L2").ToString
Dim LSArraytemp As String
i += 1
LSArray(i) = String.Concat(var2, ",", var3)
'LSArraytemp = LSArray(i)
LSArraytemp = "texttexttext"
cmnd = New SqlCeCommand("INSERT INTO LSArray(Week, LSArrValue, LS1, LS2) VALUES (" & var1 & ",'" & LSArraytemp & "'," & var2 & "," & var3 & ")", MyConnection)
cmnd.ExecuteNonQuery()
Loop
Thanks for the help guys. I do appreciate it very much! :)
Wednesday, January 27, 2010 6:47 PM
Not quite sure what you are after as a final result, does this help the situation?
LSArray(i) = String.Format("{0}, {1}", var2, var3)
jon.stromer.galley
Wednesday, January 27, 2010 6:49 PM
That was quick.
Sadly, no, nothing appears to happen. :/
Wednesday, January 27, 2010 7:24 PM | 2 votes
Did you try setting a debug breakpoint in code and examining the values while you step through the code?
BTW, if var2 and var3 are string variables then they would be the following:
var2 = "3"
var3 = "5"
I ran the following code (hard-coded values):
String.Concat("3",",","5")
...and the result was:
"3,5"
Paul ~~~~ Microsoft MVP (Visual Basic)
Wednesday, January 27, 2010 7:42 PM | 1 vote
I did the same as Paul
works fine
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim number1 As Integer = 50
Dim number2 As Integer = 100
Dim var1 As String = "Monday"
Dim var2 As String = number1.ToString
Dim var3 As String = number2.ToString
Debug.WriteLine(String.Concat(var2, ",", var3))
End Sub
Wednesday, January 27, 2010 8:17 PM
The database hates it.
Do While rdr2.Read()
Dim var1 As String = rdr2.Item("Week").ToString
Dim var2 As String = rdr2.Item("L1").ToString
Dim var3 As String = rdr2.Item("L2").ToString
Dim LSArraytemp As String
i += 1
LSArray(i) = String.Concat(var2, ",", var3)
LSArraytemp = LSArray(i)
cmnd = New SqlCeCommand("INSERT INTO LSArray(Week, LSArray, LS1, LS2) VALUES (" & var1 & "," & LSArraytemp & "," & var2 & "," & var3 & ")", MyConnection)
cmnd.ExecuteNonQuery()
Loop
This code generates the following error:
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
However, if I omit the "," (comma) from the String.Concat statement it works just fine. :/
Wednesday, January 27, 2010 9:21 PM
Hmmm. It seems to be something to do with this part of the code and not the concat part as I first thought:
cmnd = New SqlCeCommand("INSERT INTO LSArray(Week, LSArray, LS1, LS2) VALUES (" & var1 & "," & LSArraytemp & "," & var2 & "," & var3 & ")", MyConnection)
cmnd.ExecuteNonQuery()
I tried simply setting the variable, LSArraytemp = "randomstringtext" and it throws up this error:
A first chance exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll
The database field, "LSArray", is nvarchar if that makes any difference?
Wednesday, January 27, 2010 9:34 PM | 1 vote
The problem is that you are trying to insert a string value that contains you data separator.
Use the debugger to look at
"INSERT INTO LSArray(Week, LSArray, LS1, LS2) VALUES (" & var1 & "," & LSArraytemp & "," & var2 & "," & var3 & ")"
just before you execute the command. That should show the problem.
Try
"INSERT INTO LSArray(Week, LSArray, LS1, LS2) VALUES (" & var1 & ",""" & LSArraytemp & """," & var2 & "," & var3 & ")"
instead.
Wednesday, January 27, 2010 9:52 PM
Tried the code you suggested, Acamar, but the problem remains. I've also tried debugging with stop points and the variable values are exactly as expected. For some reason it will not write a non-numeric String to the database.
Wednesday, January 27, 2010 10:12 PM | 1 vote
String.Join?
Option Strict On
Option Explicit On
Module Module1
Sub Main()
Dim var1 As Integer = 3
Dim var2 As Integer = 5
Dim lsArray(3) As String
lsArray(0) = String.Join(",", New String() {var1.ToString(), var2.ToString()})
Console.WriteLine(lsArray(0))
End Sub
End Module
HTH
Tom Shelton
Wednesday, January 27, 2010 10:15 PM
Are you quite sure it is nVarChar? The problem you saw originally - where the string was evaluated and the stored value was the sum - indicates that it is not nVarChar but some numeric type.
Wednesday, January 27, 2010 10:23 PM
Are you quite sure it is nVarChar? The problem you saw originally - where the string was evaluated and the stored value was the sum - indicates that it is not nVarChar but some numeric type.
Yes, I'm 100% certain the Field is nvarchar. I've dropped the Table several times and rebuilt it just to make sure there wasn't a connection / data type issue. As I say, it will take a numeric value no problem at all but it will not take a string. :/
Wednesday, January 27, 2010 10:32 PM | 1 vote
I think the analysis has proved what you say - that even though the column is nVarChar it will not accept a string value. The only thing I can think of is that there is an autoupdate occurring on that field which is passing the field data through some additional processing each time the row is changed.
Wednesday, January 27, 2010 10:39 PM
That's what I was trying to say, but got my systems confused. Sorry.
Thursday, January 28, 2010 1:17 AM | 2 votes
You wouldn't need to mess with all these syntax issues if you used Command Parameters instead. See the following example:
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlcecommand.parameters(VS.80).aspx
Paul ~~~~ Microsoft MVP (Visual Basic)