I have not completed the code ...but why am i getting error inside while loop saying cannot convert string to int...when i am not converting string to int

namespace book
    {
        public partial class car : Form
        {
            public car()
            {
                InitializeComponent();
            }
            void FilCombo()
            {
                SqlConnection conn = new SqlConnection("Data Source=PRAVEEN\\SQLEXPRESS;Initial Catalog=travelbooking;Integrated Security=True");

                SqlCommand cmd = new SqlCommand("select *from Carss travelbooking.Carss", conn);

                conn.Open();

                SqlDataReader reader = cmd.ExecuteReader();

                while(reader.Read())
                {

                    string sname = reader.GetString("CarName");
                }


            }

It is because getString() accepts an integer, not a string. You pass in the zero-based reference for the column you want. Instead of passing in "CarName" pass in the position of that column.

Thankyou...but is there an alternate for getstring for directly using the column name

okay if i give column number i am getting an exception "Incorrect syntax near '.'."

Thats coming from your SQL statement. Which table are you trying to select from because it looks like you are referring to two.

This article has been dead for over six months. Start a new discussion instead.