简体   繁体   English

无法使用数据库中的数据集检索数据

[英]Cannot retrive data using dataset from database

I am writing web service method to retrieve the data of the user. 我正在编写Web服务方法来检索用户的数据。 To make clear, I have a table called User in my database, which has, ID, fName, lName, emailAddress, username, password, reg_ID. 为了清楚起见,我在数据库中有一个名为User的表,该表具有ID,fName,lName,emailAddress,用户名,密码和reg_ID。 If i used the ID to retrieve the data, it will work but instead if i use emailAddress in "where" clause, it gives me following error. 如果我使用ID来检索数据,它将可以工作,但是如果我在“ where”子句中使用emailAddress,则会出现以下错误。

Web service method Web服务方式

 [WebMethod(Description = "Returns Details of User with username")]
        public DataSet GetUser(string user)
        {
            DataSet dataSet = new DataSet();
            OleDbConnection oleConn = new OleDbConnection(connString);

            try
            {
                oleConn.Open();
                string sql = "SELECT * FROM [User] WHERE [username]=" + user;
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(sql, oleConn);
                dataAdapter.Fill(dataSet, "User");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                oleConn.Close();
            }
            if (dataSet.Tables["User"].Rows.Count <= 0)
                return null;

                return dataSet;
        }

Error 错误

System.NullReferenceException: Object reference not set to an instance of an object.
   at UserManagement.UserRegistration.GetUser(String user) in C:\Users\smartamrit\Desktop\SystemSoftware\UserManagement\UserRegistration.asmx.cs:line 170

Line 170 starts at if (dataSet.Tables["User"].Rows.Count <= 0 ) 第170行从if (dataSet.Tables["User"].Rows.Count <= 0 )开始

If the query does not return any rows, no tables are added to the DataSet. 如果查询不返回任何行,则不会将任何表添加到DataSet中。 Thus 从而

dataSet.Tables["User"]

is probably null. 可能为空。

You need to check the return value of the Fill method to ensure that rows were returned. 您需要检查Fill方法的返回值,以确保返回了行。

Per MSDN documentation the fill method returns an integer which represents: 根据MSDN文档 ,fill方法返回一个整数,表示:

The number of rows successfully added to or refreshed in the DataSet. 成功添加到数据集中或在其中刷新的行数。 This does not include rows affected by statements that do not return rows. 这不包括受不返回行的语句影响的行。

Furthermore, your query is open to sql injection (a security vulnerability). 此外,您的查询对sql注入(一个安全漏洞)开放。 You will want to change it to use parameterized queries instead. 您将需要更改它以使用参数化查询

Did you use single quotes while passing the email id or try to use like statement when passing email id. 您在传递电子邮件ID时使用单引号还是在传递电子邮件ID时尝试使用like语句。 Have you check whether any exception raises in the catch block. 您是否检查catch块中是否引发了任何异常。

The possible reason is there may occur a exception in catch block, it was suppressed and after that the if statement has executed and throwed a error that user table was not found. 可能的原因是catch块中可能发生异常,它被抑制,然后执行if语句并引发错误,即找不到用户表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM