• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 214
  • Last Modified:

select databind in code behind

hello, i need to query a table to determine if a table column is set to true. If so then i need to set the enable attribute to false so the menu link is not active. below is my code.
protected void checkProfileCompleteStatus(string userId)
    {
        //sql connection
        SqlConnection dbConnection = new SqlConnection(connectionString);
        //sql command/script
        string selectSQL = "select ProfileComplete from UserInformation where Where UserId = @UserId";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet,"UserInformation");
        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            if (theRow["ProfileComplete"].ToString != 1)//errors here and not sure how to use it
            {
                   //disable the menu link - 
                //<asp:Menu ID="Menu1"
                //<asp:MenuItem Text="Doctors" Value="Doctors">
                  //<asp:MenuItem NavigateUrl="~/secure/adddoctor.aspx" Text="Add a Doctor" Value="Add a Doctor"></asp:MenuItem>
                    //Enabled="false"
            }
        }
    }

Open in new window

0
vicomin
Asked:
vicomin
  • 23
  • 15
1 Solution
 
vicominAuthor Commented:
the db column is a bit and has one of the following values: 0,1,null. the code has 1 as true.
0
 
silemoneCommented:
you have 1 =   should be ==
0
 
silemoneCommented:
oops..i'm incorrect...
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
silemoneCommented:
if (theRow["ProfileComplete"].ToString != 1)

you're comparing a string and a int/ or bit value...

you need to convert them  or place quotations around 1 like "1"
0
 
silemoneCommented:
like so:

if (theRow["ProfileComplete"].ToString != "1")           <--as you're changing row ToString()...

0
 
silemoneCommented:
or you can use

bool x = false;
if ((Boolean.TryParse(theRow["ProfileComplete"].ToString(), out x) != 1);

or

if (theRow["ProfileComplete"].ToString() != "1")           <--as you're changing row ToString()...

or Convert.ToBoolean method...


either of those methods should work...
0
 
vicominAuthor Commented:
that didn't seem to work.
0
 
silemoneCommented:
so to clean this up:

since you changing value to string:
you could use:

if (theRow["ProfileComplete"].ToString() != "1")  

or

you could use Boolean TryParse
bool x = false;
if ((Boolean.TryParse(theRow["ProfileComplete"].ToString(), out x) != 1)

or  you could use

if ((Convert.ToBoolean(theRow["ProfileComplete"].ToString()) != 1)

0
 
vicominAuthor Commented:
should this be done of the dataset?

if(dataSet.Tables["UserInformation"].Rows[18]["ProfileComplete"] != 1)
0
 
silemoneCommented:
you should definitely get a different error...i may have had a type before...

now to the other problem i see:  

what if the value is null?

but you're already trying to use the ToString() method and you can't convert null to a string...

so you need another if statement

if (theRow["ProfileComplete"] != null)
0
 
silemoneCommented:
'should this be done of the dataset?

if(dataSet.Tables["UserInformation"].Rows[18]["ProfileComplete"] != 1) '  <---no..why are you looping if you're going to use a row index...i would not use this and leave it as in the above...by the way, if this was correct, all it would do would shorten the code and make it less lines, but it would still provide the same affect...
0
 
vicominAuthor Commented:
what about this?

protected void checkProfileCompleteStatus(string userId)
    {
        //sql connection
        SqlConnection dbConnection = new SqlConnection(connectionString);
        //sql command/script
        string selectSQL = "select ProfileComplete from UserInformation where Where UserId = @UserId";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet,"UserInformation");

        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
        if (theRow["ProfileComplete"].ToString() == "0" || (theRow["ProfileComplete"] == null))
            {
                //disable the menu link -
                //<asp:Menu ID="Menu1"
                //<asp:MenuItem Text="Doctors" Value="Doctors">
                //<asp:MenuItem NavigateUrl="~/secure/adddoctor.aspx" Text="Add a Doctor" Value="Add a Doctor"
                //Enabled="false" ></asp:MenuItem>
 
            }
        }

    }
0
 
silemoneCommented:
no...
have to check for null first

because if its null, and you're using || or &&, it will still check check the second condition...

i.e  it's null, it will check both conditions and still try to change null ToString()...
place if on the outside of code...

if (theRow["ProfileComplete"].ToString() == "0" || (theRow["ProfileComplete"] == null))
0
 
silemoneCommented:
if (theRow["ProfileComplete"].ToString() == "0" || (theRow["ProfileComplete"] == null))

better explanation:  here even if you place null in front as first check, the program will still check the other condition...
foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
          if (theRow["ProfileComplete"] != null)
          {
               bool isNum = false;
               if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) != 1)//errors here and not sure how to use it
               {
                   //disable the menu link -
                //<asp:Menu ID="Menu1"
                //<asp:MenuItem Text="Doctors" Value="Doctors">
                  //<asp:MenuItem NavigateUrl="~/secure/adddoctor.aspx" Text="Add a Doctor" Value="Add a Doctor"></asp:MenuItem>
                    //Enabled="false"
                }
           }
        }


}
0
 
vicominAuthor Commented:
error Operator '!=' cannot be applied to operands of type 'bool' and 'int'

errors here
if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) != 1)

0
 
silemoneCommented:
oops... change 1 = true in the code...i.e.   != true  that is what 1 should equal, yes?  1 = true ?

otherwise you should use Convert.ToBit(1)
0
 
silemoneCommented:
excuse me...not Convert.ToBit, but Convert.ToBoolean(1)
0
 
vicominAuthor Commented:
the following code is compiling but don't think it is working

protected void checkProfileCompleteStatus(string userId)
    {
        SqlConnection dbConnection = new SqlConnection(connectionString);
        string selectSQL = "select ProfileComplete from UserInformation Where UserId ='" + userId + "'";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet,"UserInformation");

        
        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            if (theRow["ProfileComplete"] != null)
            {
                if (theRow["ProfileComplete"].ToString() == "0")//errors here and not sure how to use it
                {
                    Response.Write("Enable should be false");
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;

                }
            }
        }


    }

Open in new window

0
 
silemoneCommented:
the following code is compiling but don't think it is working<-- very general statement...:0)...what makes you think its not working?  place breakpoints  with  F9 in visual studios, run while debugging (if your config files has to be updated, say yes), and f11 to see each line and its value...this will help you with your debugging skills if you don't know how to use the IDE for debugging yet...
0
 
silemoneCommented:
try debuggin this too

protected void checkProfileCompleteStatus(string userId)
    {
        SqlConnection dbConnection = new SqlConnection(connectionString);
        string selectSQL = "select ProfileComplete from UserInformation Where UserId ='" + userId + "'";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet,"UserInformation");

        
        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            if (theRow["ProfileComplete"] != null)
            {
                 bool isNum = false;
               if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) != Convert.ToBoolean(1))//errors here and not sure how to use it
                {
                    Response.Write("Enable should be true");
                    Menu1.FindItem("Doctors").Enabled = true;
                    Menu1.FindItem("Patients").Enabled = true;
                    Menu1.FindItem("Requisitions").Enabled = true;

                }
            }
        }


    }

Open in new window

0
 
silemoneCommented:
oops in the above i changed things to true, but it should still read false...but that should work...i tested it...
0
 
vicominAuthor Commented:
so if the value is not null and the value is 0 the menu items should be disabled, right

so if it is not null and the value is 1 then the menu items should be enabled, right
0
 
silemoneCommented:
yes...that's what you're going for...i can only check the logic of the code...not the data...or actual menu items...i just set some divs text to show true...
0
 
silemoneCommented:
looks like if you want the value to enable otherwise, you need and else statement after checking whether val == 0  or val != 1
0
 
vicominAuthor Commented:
looks like it is getting there
protected void checkProfileCompleteStatus(string userId)
    {
        SqlConnection dbConnection = new SqlConnection(connectionString);
        string selectSQL = "select ProfileComplete from UserInformation Where UserId ='" + userId + "'";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet, "UserInformation");

        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            bool isNum = false;
            if (theRow["ProfileComplete"] == null)
            {
                //nothing happens
                Response.Write("null");
                Menu1.FindItem("Doctors").Enabled = false;
                Menu1.FindItem("Patients").Enabled = false;
                Menu1.FindItem("Requisitions").Enabled = false;
            }
            if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(0))
            {
                //this happens if the column value is null
                Response.Write("0");
                Menu1.FindItem("Doctors").Enabled = false;
                Menu1.FindItem("Patients").Enabled = false;
                Menu1.FindItem("Requisitions").Enabled = false;
            }
            if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(1))
            {
                //this happens if the column value if 0 or 1
                Response.Write("1");
                Menu1.FindItem("Doctors").Enabled = true;
                Menu1.FindItem("Patients").Enabled = true;
                Menu1.FindItem("Requisitions").Enabled = true;
            }
        }
    }

Open in new window

0
 
vicominAuthor Commented:
so i modified the code a little to this one and got similar results
protected void checkProfileCompleteStatus(string userId)
    {
        SqlConnection dbConnection = new SqlConnection(connectionString);
        string selectSQL = "select ProfileComplete from UserInformation Where UserId ='" + userId + "'";
        System.Data.SqlClient.SqlCommand sqlCmd = new System.Data.SqlClient.SqlCommand(selectSQL, dbConnection);
        System.Data.SqlClient.SqlDataAdapter dataAdapter = new System.Data.SqlClient.SqlDataAdapter(sqlCmd);
        System.Data.DataSet dataSet = new System.Data.DataSet();
        dataAdapter.Fill(dataSet, "UserInformation");

        foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {

            if (theRow["ProfileComplete"] != null)
            {
                bool isNum = false;
                if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(1))
                {
                    Response.Write("1"); //if column value is 0 and if the column value is 1
                    Menu1.FindItem("Doctors").Enabled = true;
                    Menu1.FindItem("Patients").Enabled = true;
                    Menu1.FindItem("Requisitions").Enabled = true;
                }
                else if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(0))
                {
                    Response.Write("0"); //if null
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                }
                else
                {
                    Response.Write("null");
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                }
            }
        }
    }

Open in new window

0
 
silemoneCommented:
what are similar results?  
0
 
vicominAuthor Commented:
how do i write this to check the value if it is a 1 or 0?
0
 
silemoneCommented:
what's wrong with the code? have you debugged it?  you would change boolean.tryparse to int.tryparse...

and Convert.ToBoolean to Convert.toInt()...

what's happening?  you're not being very clear with you one-line messages...i don't know what problems you're having because you're not communicating them...please pass more details...
0
 
vicominAuthor Commented:
int isNum = 0;

if (int.TryParse(theRow["ProfileComplete"].ToString(), out isNum ) == Convert.ToInt32(1))

yields:  Error      1      Operator '==' cannot be applied to operands of type 'bool' and 'int'      
0
 
vicominAuthor Commented:
hey at this line:  else if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(0))

how do I know what the value is?
0
 
silemoneCommented:
i don't understand the question?  what 0 is or what 1 is?

please rephrase with more details...
0
 
vicominAuthor Commented:
I want to show different results based on if the value in the ProfileComplete column is null, 0 or 1. Right now if the value is 1 or 0 it will print the first if condition and if the value is null it will print the second if condition.


if ProfileComplete == null
 //do this
else if ProfileComplete == 0
 //do this
else if ProfileComplete == 1
 //do this
else
 //default action


current code

foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            if (theRow["ProfileComplete"] != null)
            {
                bool isNum = false;
                if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(1))
                {
                    Response.Write("1"); //prints if column value is 0 and if the column value is 1
                    Menu1.FindItem("Doctors").Enabled = true;
                    Menu1.FindItem("Patients").Enabled = true;
                    Menu1.FindItem("Requisitions").Enabled = true;
                }
                else if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(0))
                {
                    Response.Write("0"); //prints if column value is null
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                }
                else
                {
                    Response.Write("null");
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                }
            }
        }

Open in new window

0
 
silemoneCommented:
i see what you want...only thing is what do you want it to do
foreach (DataRow theRow in dataSet.Tables["UserInformation"].Rows)
        {
            if (theRow["ProfileComplete"] != null)
            {
                bool isNum = false;
                if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(1))
                {
                    Response.Write("1"); //prints if column value is 0 and if the column value is 1
                    Menu1.FindItem("Doctors").Enabled = true;
                    Menu1.FindItem("Patients").Enabled = true;
                    Menu1.FindItem("Requisitions").Enabled = true;
                }
                else if (Boolean.TryParse(theRow["ProfileComplete"].ToString(), out isNum) == Convert.ToBoolean(0))
                {
                    Response.Write("0"); //prints if column value is null
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                }
                else
		{	
                    Response.Write("Error. Neither null, 1, nor 0);
                    Menu1.FindItem("Doctors").Enabled = false;
                    Menu1.FindItem("Patients").Enabled = false;
                    Menu1.FindItem("Requisitions").Enabled = false;
                    //set to false
		}
            }
	    else
            {
                Response.Write("null");
                Menu1.FindItem("Doctors").Enabled = false;
                Menu1.FindItem("Patients").Enabled = false;
                Menu1.FindItem("Requisitions").Enabled = false;
            }
        }

Open in new window

0
 
vicominAuthor Commented:
if ProfileComplete == null
 //disable the menu items
else if ProfileComplete == 0
 //disable the menu items or logout as the value should never be 0
else if ProfileComplete == 1
 //enable the menu items
else
 //logout - must be an error
0
 
silemoneCommented:
i don't know to log you out...
i guess you would just redirect to a logout landing page...that page would ideally have a link that says

if you want to login again press here <---takes you to the login page...
0
 
vicominAuthor Commented:
yes - will just do a redirect - i can handle that part
0
 
silemoneCommented:
did you wrap this one up by adding the re-direct?  don't forget if you're using sessions and capturing userId after login, to also make that session = "" or null when you go to the logout page...then if you try to go backwards, the page (which theoretically should check for a session with userId in it or redirect to login page) will redirect to login page...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 23
  • 15
Tackle projects and never again get stuck behind a technical roadblock.
Join Now