im getting this error while trying to connect a mysql database to an editor, here is the code behind:
在尝试将mysql数据库连接到编辑器时,我得到了这个错误,下面是代码:
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
MySqlConnection conn = new MySqlConnection(@"connection string");//tested and working
conn.Open();
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.SelectCommand = cmd;
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Editor1.Content = dt.Rows[0]["tes"].ToString();
}
conn.Close();
}
and here is the aspx page code:
这里是aspx页面代码:
<body>
<form id="form1" runat="server">
<cc1:Editor ID="Editor1" runat="server" Height="400px" Visible="true" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</form>
what am i doing wrong, thanks in advance.
我做错了什么,提前谢谢。
i am using asp.net 3.5.
我正在使用asp.net 3.5。
3 个解决方案
#1
24
Change the line
改变行
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
To
来
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins", conn);
And it should work.
它应该工作。
Alternatively assign conn to the cmd.Connection property.
或者将conn分配给cmd。连接属性。
The problem with your code is that you never assign a connection to the command, hence the error saying that the connection is not initialized.
代码的问题是从来没有为命令分配一个连接,因此出现了一个错误,说连接没有初始化。
#2
2
Try This
试试这个
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins",conn);
or
或
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
cmd.Connection=conn;
#3
0
I got also this problem. after search the problem found that the object of connection that i have provide SqlDataAdapter is not Initialize so the connection passed as null
我也遇到了这个问题。搜索之后,问题发现我提供的SqlDataAdapter的连接对象没有初始化,因此连接被传递为null
#1
24
Change the line
改变行
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
To
来
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins", conn);
And it should work.
它应该工作。
Alternatively assign conn to the cmd.Connection property.
或者将conn分配给cmd。连接属性。
The problem with your code is that you never assign a connection to the command, hence the error saying that the connection is not initialized.
代码的问题是从来没有为命令分配一个连接,因此出现了一个错误,说连接没有初始化。
#2
2
Try This
试试这个
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins",conn);
or
或
MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
cmd.Connection=conn;
#3
0
I got also this problem. after search the problem found that the object of connection that i have provide SqlDataAdapter is not Initialize so the connection passed as null
我也遇到了这个问题。搜索之后,问题发现我提供的SqlDataAdapter的连接对象没有初始化,因此连接被传递为null