SQL SelectCommand 属性在调用“Fill”之前尚未初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2044466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The SelectCommand property has not been initialized before calling 'Fill'
提问by Lee Hull
The SelectCommand property has not been initialized before calling 'Fill'
SelectCommand 属性在调用“Fill”之前尚未初始化
I am getting this error when running StoredProcedure.ExecuteDataSet();
运行 StoredProcedure.ExecuteDataSet() 时出现此错误;
DataSet ds= new DataSet();
SqlDataAdapter ada = new SqlDataAdapter();
try
{
ada.Fill(ds);
}
catch { }
回答by Lee Hull
I was able to fix this by adding the following code:
我能够通过添加以下代码来解决这个问题:
[162] DbDataAdapter da = Factory.CreateDataAdapter();
[163] da.SelectCommand = cmd;
<-- add this[164] da.Fill(ds);
[162] DbDataAdapter da = Factory.CreateDataAdapter();
[163] da.SelectCommand = cmd;
<-- 添加这个[164] da.Fill(ds);
Hope this helps if anyone else had this problem...
如果其他人遇到这个问题,希望这会有所帮助......
回答by Carlitta87
i was having this problem and that line made it work....
我遇到了这个问题,那条线使它起作用了....
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