469,275 Members | 1,823 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

VB.NET: syntax to insert value into an array

seshu
156 100+
Hi everybody
This is seshu to explain more clearly about the above title i have a table in database now i want get a column from that table with all the rows for suppose the comlumn is containg the name i want send all those to an array in my fron end and after that on a button click i want to send all the names in array in to another table in my database i hope someone will help me atleast with giving input to array and retrive the data from array
thanking you
regards
seshu
Jan 12 '07 #1
7 16277
Killer42
8,435 Expert 8TB
Well, I haven't tested it, but I think this little self-contained routine will add a string to an array...
Expand|Select|Wrap|Line Numbers
  1. Public Sub AddStringToArray(ByVal strEntry As String, strArray() As String)
  2.   Dim Upper As Long, Lower As Long
  3.   Upper = UBound(strArray)
  4.   Lower = LBound(strArray)
  5.   ReDim Preserve strArray(Lower To Upper + 1)
  6.   strArray(Upper + 1) = strEntry
  7. End Sub
Note that the array must be defined in such a way that the size can be changed, for this to work. But anyway, even if you don't use the routine, the code should be useful as an example of adding to an array.
Jan 12 '07 #2
willakawill
1,646 1GB
Hi everybody
This is seshu to explain more clearly about the above title i have a table in database now i want get a column from that table with all the rows for suppose the comlumn is containg the name i want send all those to an array in my fron end and after that on a button click i want to send all the names in array in to another table in my database i hope someone will help me atleast with giving input to array and retrive the data from array
thanking you
regards
seshu
Is this an array or a recordset that you want? Do you want to return the data to your app so that you can access it, sort it etc or do you specifically want it to be transferred into an array?

A recordset object will hold the data and you can create an array from it if you really need to using the GetRows() method if you declare a variant.

Expand|Select|Wrap|Line Numbers
  1. Dim MyArray As Variant
  2.  
  3. MyArray = MyRecordset.GetRows()
Jan 12 '07 #3
seshu
156 100+
thanks to both of you and iam sorry for delaying in watching your sugesions ofcourse i dint check yet the moment i check this i will report you
thank you
regards
seshu
Jan 12 '07 #4
seshu
156 100+
sorry sir i dint find the way to impleement i mean i dint understand atall can you give me detail coding and one more thing i dont know how to asign this array to a column and the my application is in vb.net
Jan 12 '07 #5
hariharanmca
1,977 1GB
sorry sir i dint find the way to impleement i mean i dint understand atall can you give me detail coding and one more thing i dont know how to asign this array to a column and the my application is in vb.net

Dim strName() As String

Private Sub Load_dbNames_TO_Array()
Dim rst As ADODB.Recordset
Dim lngRecordCount As Long
Dim i As Long
strsql = "Select Names from Table1"
Set rst = dbConnection.Execute(strsql)
If rst.recordcount > 0 Then
lngRecordCount = rst.recordcount - 1
ReDim strName(0 To lngRecordCount) As String
For i = 0 To lngRecordCount
strName(i) = rst.field("Names").Value
rst.MoveNext
Next i
End If
End Sub

Private Sub Load_strNames_To_Database()
Dim rst As ADODB.Recordset
Dim lngArrayCount As Long
Dim i As Long
lngArrayCount = UBound(strName)
For i = 0 To lngArrayCount
strsql = "Insert into Table2 (Names) values('" & strName(i) & "')"
dbConnection.Execute strsql
Next i
End Sub

Private Sub Command1_Click()
Load_strNames_To_Database
End Sub

Private Sub Form_Load()
Load_dbNames_TO_Array
End Sub


I think this will solve you
Jan 12 '07 #6
willakawill
1,646 1GB
Dim strName() As String

Private Sub Load_dbNames_TO_Array()
Dim rst As ADODB.Recordset
Dim lngRecordCount As Long
Dim i As Long
strsql = "Select Names from Table1"
Set rst = dbConnection.Execute(strsql)
If rst.recordcount > 0 Then
lngRecordCount = rst.recordcount - 1
ReDim strName(0 To lngRecordCount) As String
For i = 0 To lngRecordCount
strName(i) = rst.field("Names").Value
rst.MoveNext
Next i
End If
End Sub

Private Sub Load_strNames_To_Database()
Dim rst As ADODB.Recordset
Dim lngArrayCount As Long
Dim i As Long
lngArrayCount = UBound(strName)
For i = 0 To lngArrayCount
strsql = "Insert into Table2 (Names) values('" & strName(i) & "')"
dbConnection.Execute strsql
Next i
End Sub

Private Sub Command1_Click()
Load_strNames_To_Database
End Sub

Private Sub Form_Load()
Load_dbNames_TO_Array
End Sub


I think this will solve you
This is not a good idea. Using the recordcount property will give different results depending on the recordset type, cursor type etc. You could have a million records and get -1 for the recordcount property as shown.

You are also limited here to a single column at a time. You can capture all of the columns of a table with GetRows()


Expand|Select|Wrap|Line Numbers
  1. Dim MyArray As Variant
  2.  
  3. If Not rs.EOF Then
  4.    MyArray = rs.GetRows()
  5. End If
Jan 12 '07 #7
seshu
156 100+
sir here at this place i dont have vb6 and i am strugling to convert the code into vb.net how is it in .net
regards
seshu
Jan 12 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

37 posts views Thread by Eric | last post: by
5 posts views Thread by amitbadgi | last post: by
2 posts views Thread by JJA | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.