Click here to Skip to main content
15,883,817 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,

I have created a table in databse(Username,Password,Rating as fields)..when a user logins and if he has already given rating, then the
'GiveRating' button must be disabled...if he did not gave any rating before then that button must be enabled.....But in my program,the button is disabled,though the user haven't given the rating....Below is my code...

protected void Page_Load(object sender, EventArgs e)
    {
        string name = Session["id"].ToString();
        string pwd = Session["pswd"].ToString();
        dt = ObjBRating.CheckRating(name, pwd);
        int i = 0;
       
        if(dt.Rows[i][0] !== string.Empty)
       //if(dt.Rows.Count>0)
    
        {

          btnGiveRating.Enabled = false;

        }
        else
        {
           btnGiveRating.Enabled = true;
        }
    }


Is this correct?

[edit]Tags, code block, general tidy up and spelling[/edit]
Posted
Updated 3-Aug-10 0:29am
v2
Comments
thatraja 3-Aug-10 7:04am    
Try if(dt.Rows[i][0].ToString() != string.Empty)

I assume ObjBRating.CheckRating(name, pwd); is correctly looking up your user in the DB? If so, then if your code compiled - which it won't have, !== is not an operator - then I assume that it is returning more than one row. Since you only ever check the first row, That would cause the effect you see.
 
Share this answer
 
Comments
aleaswathi 3-Aug-10 6:43am    
hello OriginalGriff,
it is looking up for the correct user in the database....if(dt.Rows[i][0] !== string.Empty)...if !== is not an operator ,den i also tried with if(dt.Rows.Count>0)....its displaying the button disabled....
Write a method that returns boolean value IsUserAlreadyRated()

Hope you know how to create connections and other part of ADO.Net codes to handle database operations.

Add parameters to SqlCommand object

C#
cmd.Parameters.Add("@UserName", SqlDBType.Varchar).Value = name;
cmd.Parameters.Add("@Password", SqlDBType.Varchar).Value = pwd;
cmd.Parameters.Add("@ReturnValue", SqlDBType.Int).Direction = ParameterDirection.ReturnValue;


Execute the following stored procedure

SQL
-----------------------------------
Create Procedure usp_IsUserRatedAlready
(
 @UserName Varchar(20),
 @Password Varhar(20)
)
As
Begin

Declare @ReturnValue Int
Set @ReturnValue = 0
--I assume rating is Integer

Select @ReturnValue = IsNull(Rating , 0) From TableName Where UserName = un and Password = pwd

--So, if rating did not happen, then definitely the result is ZERO

If @ReturnValue = 0
 Return 0;
Else
 Return 1;

End
----------------


C#
bool IsRated = Convert.ToBoolean(cmd.Parameters["@ReturnValue"].Value);

return IsRated;


On Page_Load, call this method.

C#
btnGiveRating.Enabled = IsUserAlreadyRated();


This isolates the code of checking and also easily manageable.

Hope it helps!! :)

[Modified: added pre tags]
 
Share this answer
 
v2
Comments
William Winner 3-Aug-10 13:06pm    
you can use pre tags in an answer to format the code.
There are some things that you need to check. First, OriginalGriff is right. Unless you defined your own operator !== is not an operator and I'm not sure how you compiled your code.

But if you're not getting any rows returned from dt and you are expecting one, then you need to look at your CheckRating routine to see why.

Also, if dt does not have any rows, then dt.Rows[0][0] should throw an error saying that object does not exist.

I also agree somewhat with Senthil. You should have created the method CheckRating to simply return a bool value. In that method you could have set it up to use ExecuteScalar with a parameterized query and just checked to see if the query returned anything.
 
Share this answer
 
Comments
raju melveetilpurayil 3-Aug-10 13:58pm    
William Winner me too have doubts about that line
if(dt.Rows[i][0]!=string.Empty)
when I check with VS2005/2008 it returns only this warning "Possible unintended reference comparison; to get a value comparison, cast the left hand side to type 'string'".

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