Click here to Skip to main content
15,904,986 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
Error:-CommandText property has not been initialized...
mu code is:=
C#
protected void ImageButtonLogin_Click(object sender, ImageClickEventArgs e)
   {
       try
       {


               con = new SqlConnection();
               con = aa.sqlcon();
               if (con.State == ConnectionState.Closed)
               {
                   con.Open();

               }
               string strqry = "";

               if (RadioButtonLoginEmail.Checked)
               {
                   strqry = "select User_Name,Password,status,Reg_as,Vstatus from User_Details where Email_Id='" + txtusername.Text.Trim() + "' and Password='" + txtpassword.Text.Trim() + "'";

               }
               SqlDataAdapter sda = new SqlDataAdapter(strqry, con);
               DataSet ds = new DataSet();
               sda.Fill(ds);************from this line it jumb directly into catch****
               if (ds.Tables[0].Rows.Count > 0)
               {
                   Session["uid"] = ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString();
                   Session["pass"] = ds.Tables[0].Rows[0].ItemArray.GetValue(1).ToString();
                    Session["usertype"] = ds.Tables[0].Rows[0].ItemArray.GetValue(3).ToString();

                   //string ss = Session["uid"].ToString();
                   //  string strcmd = "update Login set username='" + Session["uid"] + "',logged_in_date='" + System.DateTime.Now.ToString() + "' where username='" + Session["uid"] + "' ";
                   //  SqlCommand cmd = new SqlCommand(strcmd, con);
                   //   cmd.ExecuteNonQuery();
                    bool Vstatus = Convert.ToBoolean(ds.Tables[0].Rows[0].ItemArray.GetValue(4).ToString());
                    if (Vstatus == false)
                    {

                        // Response.Redirect("http://localhost:49504/mapsite/home.aspx", false);
                 Response.Redirect("verify.aspx", false);
                        return;
                    }
                   bool status_user = Convert.ToBoolean(ds.Tables[0].Rows[0].ItemArray.GetValue(2).ToString());

                   if (status_user == true)
                   {
                       Session["status"] = "active";
                       // Response.Redirect("http://localhost:49504/mapsite/home.aspx", false);


                       if (Session["usertype"].ToString() == "Candidate")
                       {
                           if (Session["redirecturl"] != null)
                           {
                               Response.Redirect(Session["redirecturl"].ToString(), false);
                               Session["redirecturl"] = null;
                               return;
                           }
                           Response.Redirect("UserHome.aspx", false);
                       }
                       if (Session["usertype"].ToString() == "Company")
                       {
                          Response.Redirect("CompIntrestedCandidates.aspx?intrestType=Comp_Int_Job", false);
                       }


                       if (Session["usertype"].ToString() == "Institute")
                       {
                           Response.Redirect("InstHome.aspx", false);
                       }
                       if (Session["usertype"].ToString() == "Franchise")
                       {
                           Response.Redirect("FrnchHome.aspx", false);
                       }
                       if (Session["usertype"].ToString() == "Consultant")
                       {
                           Response.Redirect("ConsMchCompForProj.aspx", false);
                       }
                   }
                   else
                   {
                       Session["status"] = "inactive";

                       if (Session["usertype"].ToString() == "Candidate")
                       {
                           Response.Redirect("PostResume.aspx", false);
                       }
                       else if (Session["usertype"].ToString() == "Company")
                       {
                           Response.Redirect("RegisterCompanies.aspx", false);
                       }
                       else if (Session["usertype"].ToString() == "Consultant")
                       {
                           Response.Redirect("RegisterConsultant.aspx", false);
                       }
                       else if (Session["usertype"].ToString() == "Franchise")
                       {
                           Response.Redirect("RegisterToOfferFranchise.aspx", false);
                       }
                       else if (Session["usertype"].ToString() == "Institute")
                       {
                           Response.Redirect("RegisterInstitute.aspx", false);
                       }

                        else if (Session["usertype"].ToString() == "Consultant")
                       {
                           Response.Redirect("RegisterConsultant.aspx",false);
                       }
                 }
               }
               //else
               //{
               //    lblres.Text = "Error!Username or password incorrect.";
               //}
               //Response.Redirect("home.aspx", false);
           }

       catch (Exception ex)
       {
           Session["uid"] = "";
           Session["pass"] = "";
           Session["status"] = "";
           lblres.Text = "Error occured!login failed.";
           // ErrorSignal.FromCurrentContext().Raise(err);

       }

so help me...
Posted
Updated 15-Jun-12 21:51pm
v2
Comments
AmitGajjar 16-Jun-12 2:29am    
when you post any question please make sure,
1) proper language
2) proper format
3) post your code after primary findings.

It looks like RadioButtonLoginEmail.Checked returned false and thus no query was formed for the string strqry.

This lead to execute an adapter that had no query defined. Internally, the first parameter is selectCommandText: A String that is a Transact-SQL SELECT statement or stored procedure to be used by the SelectCommand property of the SqlDataAdapter.
Refer: MSDN: SqlDataAdapter Constructor (String, String)[^]


Just make sure the query is formed properly and passed to adapter for execution.
 
Share this answer
 
Just think a bit before asking a question: the issue is about the property CommandText, and you show a pretty big fragment of unrelated code, but not a single like showing the name CommandText! Do you think this is nice? Or can make any sense?

Find the problem by yourself, it's one of the easiest to find. You simply try to use this property when it's value is null. Find all places where you use it and put a breakpoint in the like before, run it under debugger and find out where the property is null before use. Accordingly, the fix will either make sure the property (the field behind) is initialized before use, or you check if for null before use and don't use it if it is null (don't try to de-reference the null reference).

Now, I don't believe a more or less big application written in such manner can ever work. Look, you have written the literal "Consultant" at least 3 times, as well as other immediate string constants. This is opposite to what is called programming. Do you think such things can be supported? You will get lost in early stages of development. Please see:
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

—SA
 
Share this answer
 
Comments
Sandeep Mewara 16-Jun-12 3:59am    
Code sample is ok SA. SQLDataAdapter does not need an explicit definition of CommandText.

See my answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900