【问题标题】:SelectCommand.Connection property has not been initializedSelectCommand.Connection 属性尚未初始化
【发布时间】:2012-05-26 07:49:15
【问题描述】:

好的,我遇到了逻辑流问题。由于某种原因,我声明的变量之一最终不会在当前上下文中存在。错误在第 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" -->

【问题讨论】:

    标签: c# asp.net mysql datatable odbc


    【解决方案1】:

    看看usings 周围的括号。内层,紧随其后

        myAdapter.SelectCommand = com;
    } <- this one!
    

    释放你的命令。

    【讨论】:

    • 谢谢!稍后我会将您的答案标记为正确。
    猜你喜欢
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2011-08-10
    • 2014-04-20
    • 1970-01-01
    • 2015-07-01
    相关资源
    最近更新 更多