Hi Guys

I have a similer code which works on button click event and not working while Accessing data through data reader ?what would be the mistake ?

thanks
regards
sumith

---Database Part

    private void PopScheduleByID()
        {
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("select* from ClassSchedule where Schedule_id=@Schedule_id", con);
                cmd.Parameters.AddWithValue("@Schedule_id", txtschid.Text);
                con.Open();
                SqlDataReader DR = cmd.ExecuteReader();
                while (DR.Read())
                {
                    txtschid.Text = DR["Schedule_id"].ToString();
                    txtcourseid.Text = DR["CourseID"].ToString();

                    string Selectedday = DR[2].ToString();
                    int y = cboweekdays.FindStringExact(DR["Day"].ToString());
                    cboweekdays.SelectedIndex = y;

                    mskstarttime.Text = DR["StartTime"].ToString();
                    mskendtime.Text = DR["EndTime"].ToString();


                }
            }
        }

            ---Button Click event


              private void button1_Click(object sender, EventArgs e)
        {
            int x=cboweekdays.FindStringExact("SATURDAY");
            cboweekdays.SelectedIndex = x;
        }

have you tried inserting a breakpoint and checking the values while the program is running?

Yes in the Database Access Sql it is only getting -1 as the index number so combo value doesn't change

Please note that FindStringExact matches case where FindString does not.

I think your only real problem is that (DR["Day"].ToString() is not matching the case. and in your click event you Specify "SATURDAY" so just try FindString instead and see if it works or DR["Day"].ToString().ToUpper()

Regards

Hi

I tried that also but did not work here is my table content

(Schedule_id ,CourseID,Day StartTime EndTime ClassSerNo InsSerNo5)
(0001 FRIDAY 12:00:00 16:00:00 005 0005 )
according to the table i should get FRIDAY
,this lokks strange to me now

thanks
regards
sumith

Okay so when you step your code what is the value of DR["Day"].ToString() when you add a watch

Hi
It says FRIDAY

thanks
regards
sumith

Okay then my only suggestion is this

The code on this line isn't assigning y to the proper index
int y = cboweekdays.FindStringExact(DR["Day"].ToString());

If you step this line y = -1 correct?
and it should for instance be 2 the index of "FRIDAY"

So either "FRIDAY" had its index changed in some weird way or cboweekdays.FindStringExact is not locating "FRIDAY" in the collection (this is what i reckon)

Also try whilst stepping the code to Highlight the combox and then click Add Watch. Break down into the combox until you find items and ensure that "FRIDAY" is contained in the collection at that point in code.

It's trying to match the string Exact so make sure that the value "FRIDAY" in the ComboBox and "FRIDAY" from DR["Day"].ToString() match in every sense of the word. And that there are no trailing whitespaces or anything along those lines. try using the text visualizer to accomplish this.

I have no further suggestions
Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.