SelectCommand.Connection属性尚未初始化

时间:2012-05-18 18:10:05

标签: c# asp.net mysql datatable odbc

好吧,我遇到逻辑流程问题。由于某种原因,我声明的变量之一并不会最终存在于当前上下文中。错误在第18行:

Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.

Source Error: 


Line 16:            myAdapter.Fill(tabledata);
Line 17:        } catch (Exception ex) {
Line 18:            throw (ex);
Line 19:        } finally {
Line 20:            con.Close();

以下是整页来源:

<% @Page Language="C#" Debug="true" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.Odbc" %>
<% @Import Namespace="System.Web.Configuration" %>
<!-- #include file="header.html" -->
<%
    string conString = WebConfigurationManager.ConnectionStrings["stampConnectionString"].ConnectionString;
    OdbcDataAdapter myAdapter = new OdbcDataAdapter();  
    DataTable tabledata = new DataTable();      
    using (OdbcConnection con = new OdbcConnection(conString)) {
        using (OdbcCommand com = new OdbcCommand("SELECT * FROM cheeseisdelicious", con)) {
            myAdapter.SelectCommand = com;
        }
        try {
            con.Open();
            myAdapter.Fill(tabledata);
        } catch (Exception ex) {
            throw (ex);
        } finally {
            con.Close();
        }
    }
    Response.Write("<table>");
    foreach (DataRow row in tabledata.Rows) {
        Response.Write("<tr>");
        foreach (var item in row.ItemArray) {
            Response.Write("<td>" + item + "</td>");
        }
        Response.Write("</tr>");
    }
    Response.Write("</table>");

%>
<!-- #include file="footer.html" -->

1 个答案:

答案 0 :(得分:4)

查看usings周围的括号。

之后的内在
    myAdapter.SelectCommand = com;
} <- this one!

处理你的命令。