Avatar of SimonPrice33
SimonPrice33
 asked on

c# datarow item to string

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#?
C#.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
plusone3055

8/22/2022 - Mon
plusone3055

foreach (DataRow dr in dt.Rows) {
      CSVString.Append(dr[7] + ",");

}
SimonPrice33

ASKER
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?
plusone3055

my apologies Id have to see the rest of the code :)
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SimonPrice33

ASKER
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.
plusone3055

okay
SimonPrice33

ASKER
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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER CERTIFIED SOLUTION
Fernando Soto

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SimonPrice33

ASKER
i have not 'new'ed ' it up no..

i will try that
SimonPrice33

ASKER
thank you
plusone3055

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23