c# datarow item to string

SimonPrice33
SimonPrice33 used Ask the Experts™
on
Hi Experts

I am converting from vb.net to c# and am having a bit of trouble with getting data from the datatable.

in vb i would write

For Each dr As DataRow In dt.Rows
CSVString.Append(dr.Item(7) & ",")
Next

Open in new window


How would i do this in c#?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
foreach (DataRow dr in dt.Rows) {
      CSVString.Append(dr[7] + ",");

}

Author

Commented:
Ok,

so, i had got it right... i keep on getting an error when i try and get the values from the datatable to the string builder to write the line

Object reference not set to an instance of an object.

In stepping through my code i get 1880 rows in my datatable but for some reason get this.

any ideas?
my apologies Id have to see the rest of the code :)
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

Author

Commented:
i will get that across to you in a bout 25 minutes, i m just about to get off the train and then drive home.
okay

Author

Commented:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data ;
using System.Data.Sql;
using System.Data.SqlClient;
using System.IO;


namespace csCounterWithLeadingZeros
{

    class Program
    {
        
            
        
        static void Main(string[] args)
        {

          
            DateTime date = DateTime.Now;
            int i = 45;
            string Countfmt8 = "00000000"; //  8character number with leading 0's where required
            string Countfmt7 = "0000000"; //  7character number with leading 0's where required
                        
            //Console.WriteLine(i.ToString(Countfmt8));            
            //Emir Document Header information - MUST HAVE THE CORRECT SPACING
            
            Console.WriteLine("HdrS60317.E00.C8LME.S0000" + date.ToString("ddMMyyyy") + "IFCOTC10002              N    *               ");
            Console.WriteLine("*8LME" + date.ToString("yyyy-MM-dd") + "OIFCOTC10002                             ");
            
            //Header information
            //Console.WriteLine("A Header");
            
            //Emier data here
            SqlConnection sqlconn = new SqlConnection();
            string connstr = "Data Source=<servername>;Initial Catalog=Reports;Persist Security Info=True;Integrated Security=SSPI;";
            SqlCommand sqlcmd = new SqlCommand();
            DataTable dt = new DataTable();
            sqlcmd.Connection = sqlconn;
            sqlcmd.CommandType = CommandType.Text;
            sqlcmd.CommandText = "select * from <table> ";
            
            try
            {
                sqlconn.ConnectionString = connstr;
                sqlconn.Open();

                using (SqlDataAdapter da = new SqlDataAdapter(sqlcmd))
                {

                    da.Fill(dt);

                }
                sqlconn.Close();

                if (dt.Rows.Count > 0)
                {


                    foreach (DataRow dr in dt.Rows)
                    {




                        EmirSB.Append(dr[7] + ",");
                        

                        Console.WriteLine(EmirSB);
                        sw.WriteLine(EmirSB);
                    }
                }

            }
            catch             {
                Console.WriteLine("The connection to the database has not been made");
                Console.WriteLine("Please check that you have the necessary permissions and try again");
                Console.ReadKey();
            }       

            

            //Emir Document footer information - MUST HAVE THE CORRECT SPACING
            Console.WriteLine("*8LME-END" + i.ToString(Countfmt8));
            Console.WriteLine("*8LME-END");
            Console.WriteLine("END.S60317.E00.C8LME.S0000" + i.ToString(Countfmt7) +"                                               ");
            Console.ReadKey();
                                    
        }
    }
}

Open in new window

Retired
Distinguished Expert 2017
Commented:
Hi  SimonPrice33;

In your code I see that you are using this object, EmirSB, to build a string. I am assuming that this is a StringBuilder object. Are you new-ing it up. Something like this:

StringBuilder  EmirSB = new StringBuilder();

This is not showing up in your code.

Author

Commented:
i have not 'new'ed ' it up no..

i will try that

Author

Commented:
thank you
simonPrice33

In the future respecfully, make sure you put ALL of your question in  the beginning. It is quite unfair when you question states

"How would i do this in c#?"

with a code snippit

I translate it as to the questions requirements

you ask a second question inside the first question posting more code.. someone else answers it before me and they get all the points.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial