在调用'Fill'之前,尚未初始化SelectCommand属性

时间:2010-01-11 19:49:12

标签: sql dataset fill

在调用'Fill'

之前,尚未初始化SelectCommand属性

运行StoredProcedure.ExecuteDataSet();

时出现此错误
 DataSet ds= new DataSet();
        SqlDataAdapter ada = new SqlDataAdapter();
        try
        {
            ada.Fill(ds);
        }
        catch { }

2 个答案:

答案 0 :(得分:6)

我能够通过添加以下代码来解决这个问题:

[162] DbDataAdapter da = Factory.CreateDataAdapter();
[163] da.SelectCommand = cmd; < - 添加此 [164] da.Fill(ds);

希望如果其他人遇到这个问题会有帮助......

答案 1 :(得分:3)

我遇到了这个问题而且这条线让它起作用了......

Protected Sub searchButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles searchButton.Click
    Try
        Dim dt As New Data.DataTable
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("connection1").ToString())
        Dim cmd As New SqlCommand("getAllPerson", con)
        cmd.CommandType = Data.CommandType.StoredProcedure

        cmd.Parameters.Add("@id", Data.SqlDbType.Int).Value = CInt(SearchBox.Text)

        Dim da As New SqlDataAdapter
        da.SelectCommand = cmd
        da.Fill(dt)

        fnameTextBox.Text = dt.Rows(0).Item("FName")
        lnameTextBox.Text = dt.Rows(0).Item("LName")
        dobTextBox.Text = dt.Rows(0).Item("DOB")
        addressTextBox.Text = dt.Rows(0).Item("Address")
        address1TextBox.Text = dt.Rows(0).Item("Address1")
        contactTextbox.Text = dt.Rows(0).Item("ContactNo")
    Catch ex As Exception
        MsgBox(ex.Message.ToString())
    End Try

End Sub