Foreach循环不起作用。 [英] Foreach loop doesn't works.

查看:135
本文介绍了Foreach循环不起作用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我陷入了我多次使用过的简单代码,但是现在它不起作用而且没有给出任何错误以便我能够理解实际问题的位置,我试图通过设置断点来解决这个问题但是在foreach循环中没有通过在循环中它关闭foreach行的程序。为什么这个发生了?

给我任何建议和答案。



谢谢!





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.!


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;
                }
           
        }





更新代码,通过替换comboBox1.selectedText属性来工作到comboBox1.selectedValue属性。



Updated code , It works by replacing "comboBox1.selectedText" property to "comboBox1.selectedValue" property.

推荐答案

我会猜测,如果没有命中foreach循环中的断点,那就没有行在table.Rows。



这表示你的Proc_getdetails没有从你的数据库中返回预期的数据。



comboBox1.SelectedText是否将预期提供给Proc_getdetails?



这只是猜测,但我会从那里开始。
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.






在代码中使用foreach循环之前使用检查条件。



Hi,

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

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





交叉验证输入值是否正确传递给存储过程。< br $>


谢谢,

Sisir Patro



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

Thanks,
Sisir Patro


这篇关于Foreach循环不起作用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆