Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : "Fill: SelectCommand.Connection property has not been initialized."

im getting this error while trying to connect a mysql database to an editor, here is the code behind:

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:

<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.

like image 761
Wahtever Avatar asked Apr 21 '11 22:04

Wahtever


2 Answers

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.

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.

like image 140
Rune Grimstad Avatar answered Nov 01 '22 13:11

Rune Grimstad


Try This

MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins",conn);

or

MySqlCommand cmd = new MySqlCommand("SELECT tes FROM ins");
cmd.Connection=conn;
like image 34
Palnati Avatar answered Nov 01 '22 13:11

Palnati