2

I've successfully built up my method to execute a select command. It is working fine. Then I change my code for SqlDataAdapter DA = new SqlDataAdapter();

I tried to pass SqlCommand as CommandType.Text in the parameters but I can not do it successfully. I get error. Is there any way if I can pass it as parameters. Please see my code.

Running code (aspx page code)

if ((!string.IsNullOrEmpty(user_login.Value)) && (!string.IsNullOrEmpty(user_pass.Value)))
{
        // username & password logic
        DataTable dt = new DataTable();
        string strQuery = "SELECT 1 FROM TBL_USER_INFO WHERE USERNAME = @USERNAME AND PASSWORD = @PASSWORD";

        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = user_login.Value.Trim();
        cmd.Parameters.Add("@PASSWORD", SqlDbType.VarChar).Value = user_pass.Value.Trim();

        DBConnection conn_ = new DBConnection();

        dt = conn_.SelectData(cmd);

        if (conn_.SQL_dt.Rows.Count > 0)
        {
            Response.Redirect("Home.aspx", false);
        }
    }

Successful connection class code

public DataTable SelectData(SqlCommand command)
{
    try
    {
       conn.Open();

       SqlDataAdapter DA = new SqlDataAdapter();

       command.CommandType = CommandType.Text;
       command.Connection = conn;

       DA.SelectCommand = command;
       DA.Fill(SQL_dt);

       return SQL_dt;
   }
   catch (Exception ex)
   {
       return null;
   }
   finally
   {
       conn.Close();
   }
}

How can I pass CommandType.Text as parameters for SqlDataAdapter?

Error code

public DataTable SelectData(SqlCommand command)
{
    try
    {
        conn.Open();

        SqlDataAdapter DA = new SqlDataAdapter(command.CommandType.ToString(), conn);

        // command.CommandType = CommandType.Text;
        // command.Connection = conn;
        DA.SelectCommand = command;
        DA.Fill(SQL_dt);

        return SQL_dt;
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        conn.Close();
    }
}

I am getting this error:

System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.
at System.Data.Common.DbDataAdapter.GetConnection3(DbDataAdapter adapter, IDbCommand command, String method)...

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user4221591
  • 2,084
  • 7
  • 34
  • 68

4 Answers4

2
public DataTable SelectData(string query)
        {
            DataTable dt = new DataTable();
            using (SqlConnection con = new SqlConnection("Your Connection String here"))
            {
                con.Open();
                using (SqlCommand cmd = con.CreateCommand())
                {
                    cmd.CommandText = query;
                    cmd.CommandType = CommandType.Text;
                    using (SqlDataAdapter adp = new SqlDataAdapter(cmd))
                    {
                        adp.Fill(dt);
                        return dt;
                    }
                }
            }
        }

To use:

SelectData("select * from yourTable");
Vijunav Vastivch
  • 4,153
  • 1
  • 16
  • 30
1

Reds has the answer. Just to clean the code up a little bit...

    public DataTable SelectData(string query)
    {
        using (var connection = new SqlConnection("myConnectionString"))
        using (var command = new SqlCommand(query, connection))
        using (var adapter = new SqlDataAdapter(command))
        {
            var dt = new DataTable();

            connection.Open();
            adapter.Fill(dt);

            return dt;
        }
    }
Kevin Aung
  • 803
  • 6
  • 12
0

Actually you should pass the connection object on SQLCommand.Hope it helped you

 DBConnection conn_ = new DBConnection();
SqlCommand cmd = new SqlCommand(strQuery,conn_);
Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
-1

The error that you are getting is not related to CommandType.Text, it says you have initialised the connection property of of SelectCommand. Basically you should uncomment "command.Connection = conn;" to get rid of this error. If you still face any other issue , it is better to provide those details in the questions to provide accurate answer.

Sujith
  • 1,604
  • 9
  • 16
  • I've passed connection as parameter `conn` in the method `SqlDataAdapter DA = new SqlDataAdapter(command.CommandType.ToString(), conn)` – user4221591 Jun 07 '17 at 02:57