【问题标题】:An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll There is no row at position 0System.Data.dll 中发生“System.IndexOutOfRangeException”类型的未处理异常在位置 0 处没有行
【发布时间】:2016-11-30 14:29:41
【问题描述】:
using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LMS
{
public partial class Member : Form
{
    public Member()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        CreateNew();
    }


    void CreateNew()
    {
        textBox1.Text = "";
        textBox2.Text = "";
        textBox3.Text = "";
        textBox4.Text = "";
        textBox5.Text = "";
        comboBox1.Text = "";
        comboBox2.Text = "";
        comboBox3.Text = "";
        Connection con = new Connection();
        SqlDataAdapter sda = new SqlDataAdapter("Proc_member", con.ActiveCon());
        sda.SelectCommand.CommandType = CommandType.StoredProcedure;
        DataTable dt = new DataTable();
        sda.Fill(dt);
        textBox1.Text = dt.Rows[0][0].ToString();
        textBox2.Focus();
    }

    }

    }

我无法从这个过程中获得任何价值。我以不同的形式使用了具有不同过程名称的相同代码(并且我已经保存了所有这些代码)。它工作得很好,但在其他形式下它会不断产生这个错误。我只是一个初学者,所以请以最简单的方式回答。

【问题讨论】:

  • 看起来 proc 没有返回任何结果。 textBox1.Text = dt.Rows[0][0].ToString(); 正在尝试访问不存在的记录
  • 记录在表格中

标签: c# proc-sql windowsformsintegration


【解决方案1】:

你的数据表是空的,需要先执行命令

using (SqlCommand command = new SqlCommand(sSql, con))
{
     using (SqlDataReader reader = command.ExecuteReader())
     {
          while (reader.Read())
          {     
               DataTable dtSchema = reader.GetSchemaTable();
               for (int i = 0; i < reader.FieldCount; i++)
               {
                   var fieldVal = reader.GetValue(i).ToString();
               }
          }
     }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    相关资源
    最近更新 更多