locked
how to collect datatable result in string array RRS feed

  • Question

  • User-387238622 posted

    HI coder i dont how to get datatable result in string array

    this is my code

    protected void BindModule()
        {
            try
            {
                con.Open();
                string id = Request.QueryString["pptId"].ToString();
                //string image = Request.QueryString["Image_Name"].ToString();
                //Query to get ImagesName and Description from database
                SqlCommand command = new SqlCommand("select Imageurl,pptId,Image_Name,Description from Image_Master where pptId='" + id + "'", con);
                DataTable dt = new DataTable();
                SqlDataAdapter da = new SqlDataAdapter(command);
                da.Fill(dt);
    // here i want to collect datatable data into string array and iterate that data one by one in foreach loop
                foreach (string m in collect)
                {
    
                }
                GridView1.DataSource = dt;
                GridView1.DataBind();
                GridView1.Dispose();
                con.Close();
            }
            catch (Exception ex)
            {
                Response.Write("Error occured : " + ex.Message.ToString());
            }
            finally
            {
                con.Close();
            }
        }

    thanks in advance

    Tuesday, January 7, 2014 9:22 AM

Answers

  • User-387238622 posted

    Thanks for the reply guys i use this code and it work

    List<string> result = new List<string>();
                for (int i = 0; i < dt.Rows.Count; i++)
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        var o = dt.Rows[i].ItemArray[j];
                        result.Add(o.ToString());
                        
                    }

    now i have one more question can we collect all images path from database into one column of gridview

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2014 8:48 AM

All replies

  • User-760709272 posted

    Loop through each row, for that row define an array that is the same size as the column count for the row.  Loop through each column and add that column's value to the array using

    row[column].ToString()

    http://msdn.microsoft.com/en-us/library/system.data.datatable.rows(v=vs.110).aspx

    Tuesday, January 7, 2014 9:56 AM
  • User-387238622 posted

    can you give me some sample code.

    Thanks

    Tuesday, January 7, 2014 10:53 AM
  • User-760709272 posted

    There is sample code for looping through each column of each row in the link I gave.  If you don't know how to manipulate arrays in c# then google "c# string arrays" and you'll find plenty of examples.

    Tuesday, January 7, 2014 10:58 AM
  • User697462465 posted

    Hi amiteshverma,

    According to your description, my understanding is that you would like to fetch datatable columns value into string array.

    If so, please try to refer to the following code:

    			string[] strArr = new string[dt.Rows.Count];
    			int i=0;
    			foreach (DataRow r in dt.Rows)
    			{
    				strArr[i] = r["ColumnName"].ToString();
    				i++;
    			}

    Hope it helps.

    Best Regards,
    Terry Guo

    Wednesday, January 8, 2014 1:13 AM
  • User-1998339254 posted

    Hi,

    Pls try these codes

    in dtLength=dt.Rows.Count;
    string[] strArray= new string[dtLength];
    for(int i=0; i<dtLength;i++)
    {
    strArray[i]=dt.Rows[i]["ColumnName"].ToString();
    
    }

    Wednesday, January 8, 2014 3:44 AM
  • User-387238622 posted

    Thanks for the reply guys i use this code and it work

    List<string> result = new List<string>();
                for (int i = 0; i < dt.Rows.Count; i++)
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        var o = dt.Rows[i].ItemArray[j];
                        result.Add(o.ToString());
                        
                    }

    now i have one more question can we collect all images path from database into one column of gridview

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 8, 2014 8:48 AM