kubra
kubra

Reputation: 3

ExecuteReader: CommandText property not initialized

public DataTable Liste()
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-JHLF03K\\SQLEXPRESS;Initial Catalog=OtelWebSite;Integrated Security=True");
    string sql = "";
    SqlDataAdapter dap = new SqlDataAdapter(sql, con);  
    DataTable table = new DataTable();
    con.Open();
    dap.Fill(table);

    sql += "SELECT ";
    sql += "O.Id, ";
    sql += "O.OdaTurId,";
    sql += "T.Ad AS OdaTur, ";
    sql += "O.Ad, ";
    sql += "O.KatNo, ";
    sql += "O.Aciklama, ";
    sql += "K.Tanim AS Durum ";
    sql += "FROM Oda O, Kod K,OdaTur T  ";
    sql += "WHERE O.Durum = K.Kod ";
    sql += "AND T.Id = O.OdaTurId ";
    sql += "ORDER BY O.Id,O.OdaTurId";



    con.Close();
    return table;
}

I get an error

ExecuteReader: CommandText property not initialized

on this line of code:

dap.Fill(table);

Upvotes: 0

Views: 972

Answers (1)

Dumisani
Dumisani

Reputation: 3038

You cannot execute and empty sql query. Move your sql query creation to the top before the line that creates the DataAdapter

public DataTable Liste()
{
    SqlConnection con = new SqlConnection("Data Source=DESKTOP-JHLF03K\\SQLEXPRESS;Initial Catalog=OtelWebSite;Integrated Security=True");
    string sql = "";

    sql += "SELECT ";
    sql += "O.Id, ";
    sql += "O.OdaTurId,";
    sql += "T.Ad AS OdaTur, ";
    sql += "O.Ad, ";
    sql += "O.KatNo, ";
    sql += "O.Aciklama, ";
    sql += "K.Tanim AS Durum ";
    sql += "FROM Oda O, Kod K,OdaTur T  ";
    sql += "WHERE O.Durum = K.Kod ";
    sql += "AND T.Id = O.OdaTurId ";
    sql += "ORDER BY O.Id,O.OdaTurId";

    SqlDataAdapter dap = new SqlDataAdapter(sql, con);  
    DataTable table = new DataTable();
    con.Open();
    dap.Fill(table);

    con.Close();
    return table;
}

Upvotes: 1

Related Questions