Click here to Skip to main content
15,882,329 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Im stucked in simple code which i used many times , but now it does not works and not gives any error so that i can understand where the actual problem, I tried to solve this by putting breakpoints but at foreach loop break not passes in loop it closes program at foreach line. why this happed?
give me any suggestions and answers.

Thanks.!


C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
                SqlCommand cmd = new SqlCommand("Proc_getdetails", con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@name", comboBox1.SelectedText);
                DataTable table = new DataTable();

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(table);

                foreach (DataRow row in table.Rows)
                {
                    string age = row["m_age"].ToString();
                    txtage.Text = "";
                    txtage.Text = age;
                }
           
        }


Updated code , It works by replacing "comboBox1.selectedText" property to "comboBox1.selectedValue" property.
Posted
Updated 30-Sep-15 20:54pm
v2
Comments
Naveen.Sanagasetti 1-Oct-15 2:22am    
Please check the rows in datatable, If there is no rows obviously foreach terminated. Please check and let us know
Ralf Meier 1-Oct-15 2:22am    
You should look (by Setting a Breakpoint to the programm-row with the foreach-command) if :
- the datatable is filled
- and if it's filled : a row with the name "m_age" exists
Krunal Rohit 1-Oct-15 4:05am    
you blaming foreach loop :D :D

-KR

I would stand to guess, that if the break point within the foreach loop is not being hit, that there are no rows in table.Rows.

This would indicate that your Proc_getdetails is not returning the expected data from your database.

Does comboBox1.SelectedText supply the expected into to Proc_getdetails?

This is just a guess, but I would start there.
 
Share this answer
 
v2
Comments
Member 11543226 1-Oct-15 2:52am    
Actually the supplied value is not set in name object because combobox doesnt supply value with selectedText property when i used "selected value" property it gets proper values.
Hi,

Use a check condition before using the foreach loop in the code.

C#
if (table.Rows.Count > 0)
                {
                    foreach (DataRow row in table.Rows)
                    {
                        string age = row["m_age"].ToString();
                        txtage.Text = "";
                        txtage.Text = age;
                    }
                }


Cross verify whether the input values are passing properly or not to the stored procedure.

Thanks,
Sisir Patro
 
Share this answer
 
Comments
Member 11543226 1-Oct-15 2:52am    
Thanks for help , got solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900