Results 1 to 10 of 10

Thread: binding databse values to an array

  1. #1

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: binding databse values to an array

    VB Code:
    1. For j = 0 To ds.Tables(0).Rows.Count - 1
    2.     arr1[COLOR=Red][U](j)[/U][/COLOR] = ds.Tables(0).Rows(j)("date")
    3. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: binding databse values to an array

    hi jmcilhinney thank you very much

  4. #4

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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

  5. #5
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    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.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: binding databse values to an array

    You have to have created the array before you can set its elements. This:
    VB Code:
    1. Dim arr1() As String
    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:
    1. Dim arr1(ds.Tables(0).Rows.Count - 1) As String
    Also, can you please enclose your code snippets in VBCODE tags to improve readability?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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

  9. #9

    Thread Starter
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: binding databse values to an array

    hi jmcilhinney thank you very much i got it. nice reply

  10. #10
    Hyperactive Member tommygrayson's Avatar
    Join Date
    Aug 2005
    Location
    In my Nissan Silvia
    Posts
    433

    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:
    1. arr1(j) = ds.Tables(0).Rows(j).Item(0)

    Tell me if it works.
    Rate Me! Rate Me! Rate Me!

    Time to fly.

    Copyright GraysonSoft Inc. 2007

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width