Results 1 to 3 of 3

Thread: [RESOLVED] Retrieve value from table and How to store in string variable

  1. #1

    Thread Starter
    Junior Member ehsan85's Avatar
    Join Date
    Nov 2010
    Location
    Faridpur, Bangladesh
    Posts
    29

    Resolved [RESOLVED] Retrieve value from table and How to store in string variable

    Hello,

    I want to store some data to string array that came from Database table.



    The table Data is (only a column):

    Alice Mutton
    Aniseed Syrup
    Boston Crab Meat
    Camembert Pierrot
    Carnarvon Tigers

    ...

    ...



    I want to store this retrieved data to String array like (Programetically):

    string[] data = {"Alice Mutton", "Aniseed Syrup", "Boston Crab Meat", "Camembert Pierrot", "Carnarvon Tigers"};



    How can I fix it?



    best regards

    ehsan

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

    Re: Retrieve value from table and How to store in string variable

    Follow the CodeBank link in my signature and check out my thread on Retrieving & Saving Data. One of the examples uses a data reader to read the result set of a query one record at a time. You can create a List<string> and then read the data the same way and add each value to that list. When you're done, you can just use the List as is or, if you specifically need an array, you can call its ToArray method.
    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
    Junior Member ehsan85's Avatar
    Join Date
    Nov 2010
    Location
    Faridpur, Bangladesh
    Posts
    29

    Re: Retrieve value from table and How to store in string variable

    Finally I got it Here is the code
    Code:
    PfolioDataContext db = new PfolioDataContext();
            string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["PortfolioDBConnectionString"].ConnectionString;
            SqlDataAdapter adpt = new SqlDataAdapter("select PortID, pName from Portfolio", conStr);
            DataTable dt = new DataTable();
            adpt.Fill(dt);
    
            int TotalRow = dt.Rows.Count;
    
            string[] res = new string[TotalRow];
            for (int i = 0; i < TotalRow; i++)
            {
                res[i] = dt.Rows[i]["pName"].ToString();
            }
    This problem is solved

Tags for this Thread

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