binding databse values to an array
hi, i have table named as emp which ha one column as dob and i am retreiving dob from my tables,it has 5 rows.i wan to insert those 5 rows records into an array.
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1 = ds.Tables(0).Rows(j)("date")
Next
i used this but it shows error,soplease help me to acheive my need
Re: binding databse values to an array
VB Code:
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1[COLOR=Red][U](j)[/U][/COLOR] = ds.Tables(0).Rows(j)("date")
Next
Re: binding databse values to an array
hi jmcilhinney thank you very much
Re: binding databse values to an array
hi jmcilhinney
this is my code, i used your code it shows error as
An unhandled exception of type 'System.NullReferenceException' occurred in helper.exe
Additional information: Object reference not set to an instance of an object.
This is my coding:
dim arr1() as string
cmd = New SqlCommand("sp_sundaycheck", con)
da = New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@a", "2007-01-08")
cmd.Parameters.Add("@b", "2007-01-31")
da.Fill(ds, "good")
dim d as integer = ds.Tables(0).Rows.Count - 1
i got d values as 5 becos i retreived 5 rows recods
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1(j) = ds.Tables(0).Rows(j)("date").ToString
Next
please help me solve my error
Re: binding databse values to an array
Quote:
Originally Posted by karthikeyan
dim arr1() as string
cmd = New SqlCommand("sp_sundaycheck", con)
da = New SqlDataAdapter(cmd)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add("@a", "2007-01-08")
cmd.Parameters.Add("@b", "2007-01-31")
da.Fill(ds, "good")
dim d as integer = ds.Tables(0).Rows.Count - 1
i got d values as 5 becos i retreived 5 rows recods
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1(j) = ds.Tables(0).Rows(j)("date").ToString
Next
Hmm....
You have not Dimmed ds as New Dataset and you have not added cmd.ExecuteNonQuery.
Re: binding databse values to an array
You have to have created the array before you can set its elements. This:simply declares a variable. It does NOT create an array. You have to specify the size of the array in order for it to be created. You can't make an egg carton if you don't know how many eggs it has to hold.
VB Code:
Dim arr1(ds.Tables(0).Rows.Count - 1) As String
Also, can you please enclose your code snippets in VBCODE tags to improve readability?
Re: binding databse values to an array
Please not that you must rearrange your code so that the array is created AFTER the DataTable is populated.
Also, why are you declaring your array as type String? You're database column contains Dates so your array should contain Date objects too.
Re: binding databse values to an array
hi tommygrayson, i declared that ds as dataset. and one more thing need not to give execute non query in my code
if i give dim d as integer=ds.tables(0).rows.count i am getting d values as 5
i am getting error while enteirng to this only
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1(j) = ds.Tables(0).Rows(j)("date").ToString
Next
please help me to solve
Re: binding databse values to an array
hi jmcilhinney thank you very much i got it. nice reply
Re: binding databse values to an array
Quote:
Originally Posted by karthikeyan
]
For j = 0 To ds.Tables(0).Rows.Count - 1
arr1(j) = ds.Tables(0).Rows(j)("date").ToString
Next
You should use this instead:
VB Code:
arr1(j) = ds.Tables(0).Rows(j).Item(0)
Tell me if it works.