如何检查数据表中的值 [英] how to check value in datatable

查看:72
本文介绍了如何检查数据表中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在databse(用户名,密码,评分字段)中创建了一个表..当用户登录时,如果他已经给出评分,则
必须禁用"GiveRating"按钮...如果他在此之前未给出任何评分,则必须启用该按钮.....但是在我的程序中,该按钮被禁用,尽管用户没有给出评分....以下是我的代码...



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;
        }
    }



这是正确的吗?

[edit]标记,代码块,常规整理和拼写[/edit]



Is this correct?

[edit]Tags, code block, general tidy up and spelling[/edit]

推荐答案

我假设ObjBRating.CheckRating(name, pwd);在数据库中正确地查询了您的用户?如果是这样,那么如果您编译的代码(没有),!==不是运算符,那么我认为它返回的行多.由于您只检查第一行,因此会产生效果.
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.


编写一个返回布尔值IsUserAlreadyRated()
的方法
希望您知道如何创建连接和ADO.Net代码的其他部分来处理数据库操作.

将参数添加到SqlCommand对象

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

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

-----------------------------------
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
----------------





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

return IsRated;



在Page_Load上,调用此方法.



On Page_Load, call this method.

btnGiveRating.Enabled = IsUserAlreadyRated();



这样可以隔离检查代码,并且易于管理.

希望能帮助到你!! :)

[修改:添加了前置标记]



This isolates the code of checking and also easily manageable.

Hope it helps!! :)

[Modified: added pre tags]


您需要检查一些内容.首先,OriginalGriff是正确的.除非您定义自己的运算符,否则!==不是运算符,并且我不确定如何编译代码.

但是,如果您没有从dt返回任何行并且期望得到一行,那么您需要查看您的CheckRating例程以了解原因.
另外,如果dt没有任何行,则dt.Rows[0][0]应该抛出错误,表明该对象不存在.

我也同意圣蒂尔(Senthil).您应该已经创建了方法CheckRating来简单地返回bool值.在该方法中,您可以将其设置为对参数化查询使用,并仅检查查询是否返回了任何内容.
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.


这篇关于如何检查数据表中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆