locked
Property Has Not been Initialized RRS feed

  • Question

  • User1109811461 posted

    i got errror when i run this code below:

     private SqlCommand FormCommand()
        {
            object adminname = txtid.Text;
            object adminpass = txtpass.Text;
    
            //string commandString = "INSERT INTO Support" + "(AdminName,AdminPass)" + "VALUES (@AdminPass,@AdminPass)";
            string commandString = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            SqlCommand command = new SqlCommand(commandString);
            
            //command.CommandText = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            command.Parameters.AddWithValue("adminname", adminname);
            command.Parameters.AddWithValue("adminpass", adminpass);
    
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                //object adminname = txtid.Text;
                Session["username"] = adminname;
                Response.Redirect("RequestSupport.aspx");
            }
            else
            {
                
                lblerror.Visible = true;
                lblerror.Text = "Sila Semak Id,Katalaluan Atau Kelas Selian Anda";
    
            }
    
            return command;
    
        }
    
    
        protected void btnsubmit_Click(object sender, EventArgs e)
        {
    
            SqlCommand command = FormCommand();
            SqlConnection connection = SharedConnection.Connection;
    
    
            if ((!(connection.State == System.Data.ConnectionState.Open)))
            {
                connection.Open();
            }
    
            try
            {
                command.Connection = connection;
                command.ExecuteNonQuery();
                ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Data Has Been Save')</script>");
    
            }
            catch (Exception ex)
            {
                //Response.Redirect("Errorpage.aspx");
            }
            finally
            {
                connection.Close();
            }
           
        }


    below is the error message:

    Fill: SelectCommand.Connection property has not been initialized.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details: System.InvalidOperationException: Fill: SelectCommand.Connection property has not been initialized.
    Source Error:

    Line 35:         SqlDataAdapter da = new SqlDataAdapter(command);
    Line 36:         DataTable dt = new DataTable();
    Line 37: da.Fill(dt); Line 38:         if (dt.Rows.Count > 0)
    Line 39:         {

     

    I need help to resolve this isssues

     

    Monday, November 19, 2012 9:47 PM

Answers

  • User-2146987983 posted

    //string commandString = "INSERT INTO Support" + "(AdminName,AdminPass)" + "VALUES (@AdminPass,@AdminPass)";
            string commandString = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            SqlCommand command = new SqlCommand(commandString);
            
            //command.CommandText = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            command.Parameters.AddWithValue("adminname", adminname);
            command.Parameters.AddWithValue("adminpass", adminpass);
    
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);

    As the error suggests you have not initialized Connection property of Command object.

    da.Fill(dt); here DataAdapter needs a command with connection property initialized.

    So, there should ideally be a line that says...

    command.Connection = new SqlConnection(......); or, command.Connection = sharedConnection.Connection;

    before...you execute da.Fill(dt);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 10:55 PM

All replies

  • User-2146987983 posted

    //string commandString = "INSERT INTO Support" + "(AdminName,AdminPass)" + "VALUES (@AdminPass,@AdminPass)";
            string commandString = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            SqlCommand command = new SqlCommand(commandString);
            
            //command.CommandText = "select * from AdminLogIn where AdminName=@adminname and AdminPass=@adminpass";
            command.Parameters.AddWithValue("adminname", adminname);
            command.Parameters.AddWithValue("adminpass", adminpass);
    
            SqlDataAdapter da = new SqlDataAdapter(command);
            DataTable dt = new DataTable();
            da.Fill(dt);

    As the error suggests you have not initialized Connection property of Command object.

    da.Fill(dt); here DataAdapter needs a command with connection property initialized.

    So, there should ideally be a line that says...

    command.Connection = new SqlConnection(......); or, command.Connection = sharedConnection.Connection;

    before...you execute da.Fill(dt);

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 10:55 PM
  • User1109811461 posted

    hi me_ritz

    you resolve my problem i use as you said put command.Connection = sharedConnection.Connection;

    on top of  da.Fill(dt);

    thanks a lot's my expertiseCool

    Tuesday, November 20, 2012 12:25 AM