位置2没有行c#运行时异常

jgovgodb  于 2021-06-18  发布在  Mysql
关注(0)|答案(2)|浏览(445)

我正在从数据库检索数据并在标签中显示。但问题是,这无法检索db tabel第一行数据。它将打印第二行数据。如果我给了db第一行c\U代码,那么它会出现错误“位置2没有行”,请解决我的问题。谢谢你

  1. private void Get_Purchasing_Amount()
  2. {
  3. try
  4. {
  5. string get_P_Amount = "";
  6. double var_P_Amount = 0;
  7. int var_C_Code = 0;
  8. string query = "select c_code as 'code' from `db_vegetable`.`tbl_payment_master`";
  9. DataTable dt_C_Code = method_Class.method_Class.FetchRecords(query);
  10. if (dt_C_Code.Rows.Count > 0)
  11. {
  12. for (int i = 0; i <= dt_C_Code.Rows.Count; i++)
  13. {
  14. var_C_Code = Convert.ToInt32(dt_C_Code.Rows[i]["code"]);
  15. if (var_C_Code.Equals(Convert.ToInt32(txt_Customer_Code.Text)))
  16. {
  17. if (check_All.Checked.Equals(true))
  18. {
  19. get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' FROM `db_vegetable`.`tbl_payment_master` WHERE `c_code` = " + txt_Customer_Code.Text + "";
  20. }
  21. else
  22. {
  23. string dt_Query = "select `id` as 'id' from `db_vegetable`.`tbl_order_details`";
  24. DataTable dt_C_O = method_Class.method_Class.FetchRecords(dt_Query);
  25. if (dt_C_O.Rows.Count > 0)
  26. get_P_Amount = "SELECT IFNULL(SUM(t_price),0) as 'purchasing' FROM `db_vegetable`.`tbl_order_details` WHERE `c_code` = " + txt_Customer_Code.Text + " AND (`date` BETWEEN '" + txt_From_Date.Text + "' AND '" + txt_To_Date.Text + "')";
  27. else
  28. lbl_Purchasing_Amount.Text = "0";
  29. }
  30. DataTable dt = method_Class.method_Class.FetchRecords(get_P_Amount);
  31. var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]);
  32. lbl_Purchasing_Amount.Text = var_P_Amount.ToString();
  33. }
  34. else
  35. {
  36. lbl_Purchasing_Amount.Text = "0";
  37. }
  38. }
  39. }
  40. else
  41. {
  42. lbl_Purchasing_Amount.Text = "0";
  43. }
  44. }
  45. catch (Exception ex)
  46. {
  47. MessageBox.Show(ex.Message);
  48. }
  49. }
eoxn13cs

eoxn13cs1#

现在的问题是解决问题就是那个破关键词。

  1. private void Get_Purchasing_Amount()
  2. {
  3. try
  4. {
  5. string get_P_Amount = "";
  6. double var_P_Amount = 0;
  7. //int var_C_Code = 0;
  8. string query = "select c_code as 'code' from `db_vegetable`.`tbl_payment_master`";
  9. DataTable dt_C_Code = method_Class.method_Class.FetchRecords(query);
  10. if (dt_C_Code.Rows.Count > 0)
  11. {
  12. for (int i = 0; i <= dt_C_Code.Rows.Count; i++)//these line generate error please check this
  13. {
  14. //var_C_Code = Convert.ToInt32(dt_C_Code.Rows[i]["code"]);
  15. if (Convert.ToInt32(dt_C_Code.Rows[i]["code"]).Equals(Convert.ToInt32(txt_Customer_Code.Text)))
  16. {
  17. if (check_All.Checked.Equals(true))
  18. {
  19. get_P_Amount = "SELECT IFNULL(`purchasing`,0) AS 'purchasing' FROM `db_vegetable`.`tbl_payment_master` WHERE `c_code` = " + txt_Customer_Code.Text + "";
  20. }
  21. else
  22. {
  23. string dt_Query = "select `id` as 'id' from `db_vegetable`.`tbl_order_details`";
  24. DataTable dt_C_O = method_Class.method_Class.FetchRecords(dt_Query);
  25. if (dt_C_O.Rows.Count > 0)
  26. get_P_Amount = "SELECT IFNULL(SUM(t_price),0) as 'purchasing' FROM `db_vegetable`.`tbl_order_details` WHERE `c_code` = " + txt_Customer_Code.Text + " AND (`date` BETWEEN '" + txt_From_Date.Text + "' AND '" + txt_To_Date.Text + "')";
  27. else
  28. lbl_Purchasing_Amount.Text = "0";
  29. }
  30. DataTable dt = method_Class.method_Class.FetchRecords(get_P_Amount);
  31. var_P_Amount = Convert.ToDouble(dt.Rows[0]["purchasing"]);
  32. lbl_Purchasing_Amount.Text = var_P_Amount.ToString();
  33. break;
  34. }
  35. else
  36. {
  37. lbl_Purchasing_Amount.Text = "0";
  38. }
  39. }
  40. }
  41. else
  42. {
  43. lbl_Purchasing_Amount.Text = "0";
  44. }
  45. }
  46. catch (Exception)
  47. {
  48. }
  49. }
展开查看全部
cpjpxq1n

cpjpxq1n2#

我相信这可能就是问题所在: for (int i = 0; i <= dt_C_Code.Rows.Count; ; i++) {...} 请考虑替换 foreach (DataRow row in dt_C_Code.Rows) { ...} 如果哪一行逻辑上应该排在“第一”位很重要,那么请考虑使用 order by sql语句中的子句。

相关问题