Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please see the code:
public static string str1;
   public static string str2;
   public static string str3;
   public static string gets3pending()
   {
       string id = "";
       string Path = "";
       SqlConnection con = Database.GetConnection();
       SqlCommand com = new SqlCommand("Select id,Path from tblShareFile where status=0", con);
       DataSet ds = new DataSet();
       DataTable dt = new DataTable();

       SqlDataAdapter da = new SqlDataAdapter(com);
       //DataSet ds = new DataSet();
       da.Fill(ds, "tblShareFile");
       da.Fill(dt);
       SqlDataReader dr = com.ExecuteReader();

       while (dr.Read())
       {
           if (dt.Rows.Count > 0)
           {
               //for (int i = 0; i < dt.Rows.Count; i++)
               {
                   str1 += dr["id"].ToString() + "'";
                   str2 += dr["Path"].ToString() + "'";

               }
           }
       }

           dr.Close();

           string str3 = str1.ToString() + str2.ToString();

           return str3;


I want to create string like this: 1,path1 2,path2 3,path3 etc.
It's just to show id and path name, but it's not working.

Please, can any one help me?

Thanks in advance.
Posted
Updated 18-May-11 21:50pm
v4
Comments
saxenaabhi6 19-May-11 1:31am    
did you put the break points on str1 and str2 and watched what are the outputs for each iteration in the loop
Dalek Dave 19-May-11 3:50am    
Edited for Grammar and Readability.

Try
C#
f (dt.Rows.Count > 0)
{
//for (int i = 0; i < dt.Rows.Count; i++)
  {
     str3 + = dr["id"].ToString() + "," + dr["Path"].ToString() + " ";
  }
}
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 19-May-11 1:35am    
"+ =" will not compile. And why do you set a bad example of using repeated "+"? It's not good; use "string.Format" for clarity and performance.
--SA
Abhinav S 19-May-11 2:06am    
OP can make note of that as well.
rahul dev123 19-May-11 1:43am    
please help me providing example code for this
rahul dev123 19-May-11 1:46am    
str3 + = dr["id"].ToString() + "," + dr["Path"].ToString() + " ";
it's not supported. different errors are there
Abhinav S 19-May-11 2:05am    
This was just to give you an idea of what you need to do - it was not the exact answer.
It is good to use This

C#
StringBuilder str = new StringBuilder();
            while (dr.Read())
      {
          if (dt.Rows.Count > 0)
          {

          str.Append(dr["id"].ToString());
          str.Append(" ,");
          str.Append(dr["Path"].ToString());
          }
      }
 
Share this answer
 
you can also do it with dataset like below

C#
for(int i=0 ;i<ds.Tables[0].Rows.Count;i++)
             {
                 str1 += ds.Tables[0].Rows[i]["id"].ToString() + "," + ds.Tables[0].Rows[i]["Name"].ToString();
             }
 
Share this answer
 
public static string str1;
    public static string str2;
    public static string str3;
    public static string gets3pending()
    {
        string id = "";
        string Path = "";
        SqlConnection con = Database.GetConnection();
        SqlCommand com = new SqlCommand("Select id,Path from tblShareFile where status=0", con);
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
      
        SqlDataAdapter da = new SqlDataAdapter(com);
        //DataSet ds = new DataSet();
        //da.Fill(ds, "tblShareFile");
        da.Fill(dt);
        //SqlDataReader dr = com.ExecuteReader();     
        //while (dr.Read())
        //{            
          StringBuilder str = new StringBuilder();

          if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    str.Append(dr["id"].ToString());
                    str.Append(" ,");
                    str.Append(dr["Path"].ToString());
                }
            }
        //}          
            //dr.Close();      
            return str.ToString();
 
Share this answer
 
v2
Comments
Tarun.K.S 19-May-11 3:27am    
Right, dr.Read() is not needed as the for-loop below is iterating through each row. 5+
nit_singh 19-May-11 3:33am    
thanks..even com.ExecuteReader() is also not needed because already we filled the dt.
Dalek Dave 19-May-11 3:50am    
Good Answer.
rahul dev123 19-May-11 5:39am    
Thanks everyone

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900