Using DataTables from Database to populate program on startup without getting System.IndexOutOfRange messages

StackOverflow https://stackoverflow.com/questions/16586601

Domanda

Hi Everybody and THANK YOU IN ADVANCE!! for any and all help I am an absolute newbie to coding and programming (4-5months)

I am using visual studio 2012 and coding in C#

I am making a windows form application that connects to a database in SQL Server management studio

basically i have two combo boxes that need to be populated with the data from the db on start up.

I have created a method to populate the combo boxes and the program works 100% the boxes do populate on start up with no data missing, all rows are displayed in the combo boxes and everything does exactly what its supposed to do and when its supposed to do it...

HOWEVER!

every time i run my program i get this message 5 times (as the code that generates the message is used 5 times)

System.IndexOutOfRangeException:There is no row at position 0
  at System.Data.RBTree1.GetNodeByIndex(Int32 userIndex)
  at System.Data.DataRowCollection.get_Item(Int32 index)

BEFORE the actual program starts and runs properly.??? (its driving me nuts!!) here is all my relevant code

the code below is from my actual form as you can see i am using update_combobox(); in the initalizeComponent section to get
it to populate at start up

        namespace DB_Program
       {
           public partial class Form1 : Form
           {

                DataClass dc = new DataClass();

                public Form1()
                {
                    Thread t = new Thread(new ThreadStart(SplashScreen));
                    t.Start();
                    Thread.Sleep(5000);
                    InitializeComponent();
                    t.Abort();
                    update_ComboBox();            
                  }

here is my code from my form that makes it possible

     private void update_ComboBox()
    {
        DataSet pList = dc.PListPop();

        cboBoxPList.DataSource = pList.Tables[0];
        cboBoxPList.DisplayMember = "PName";
        cboBoxPList.ValueMember = "PName";

        DataSet devList = dl.DevListPop();

        cboBoxDev.DataSource = devList.Tables[0];
        cboBoxDev.DisplayMember = "LName";
        cboBoxDev.ValueMember = "LName";

    }


         public String getDate(String pName)
        {
            String date = null;
            DataTable dTable = new DataTable();

            try
            {
                conn.Open();
                SqlDataAdapter sda = new SqlDataAdapter("SelPro", conn);
                sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                sda.SelectCommand.Parameters.Add("@PName", SqlDbType.VarChar).Value = pName;
                sda.Fill(dTable);
                date = dTable.Rows[0]["StartDate"].ToString();

            }
            catch (Exception GSD)
            {

                string a = GSD.StackTrace.ToString();
                MessageBox.Show(GSD.ToString());
            }
            finally
            {
                if (conn != null)
                {
                    conn.Close();
                }
            }

            return date;
        }

according to my messages these are the lines that generates the messages, the first message relates to the code directly above

         date = dTable.Rows[0]["StartDate"].ToString();
         date = dTable.Rows[0]["PlannedEndDate"].ToString();
         FirstName = dTable.Rows[0]["FirstName"].ToString();
         Spec = dTable.Rows[0]["Specialty"].ToString();
         PID = (int) dTable.Rows[0]["PID"];

PLEASE PLEASE PLEASE somebody end my frustrating nightmare! i need the program to stay and run exactly as is, all i am trying to do is whatever i need to so that the program runs WITHOUT giving me those messages

once again thank you!

È stato utile?

Soluzione

Its All good please ignore this question as i have managed to solve it myself!

there is no error in the code the problem lay in the property tab settings that was generating things it shouldn't

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top