Solved

SqlDataReader is not working - does not read data

Posted on 2013-11-13
2
849 Views
Last Modified: 2013-11-18
When I debug it, it pass the loop while(readerOne.Read()). It never enter into this loop even though there is data.
What am I missing?  Hopefully, I can get some assistance soon.  It appears no one is out there because of no attention to my previous posts. I deleted them because I eventually find a solution on stackoverflow.

SqlDataReader does not data - does not read data

When I run the SQL statement from SQL Server - a result set is returned.
ID             UserName              PerLevel
1      US\\nathanm      1

sql string in debug
"select * from PermissionLevel where username = 'US\\nathanm'"

           
protected int getdata(string username)
        {
            int permissionlevel = 0;
                                   
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyTestDbConnectionString"].ConnectionString);
     

            string sql = "select * from PermissionLevel where username = '" + username + "'";
            SqlCommand sc = new SqlCommand(sql,conn);
            sc.CommandType = System.Data.CommandType.Text;
            conn.Open();

            SqlDataReader rdr = sc.ExecuteReader();
           
             while (rdr.Read())
            {
                permissionlevel = rdr.GetInt16(2);            
            }
            rdr.Close();
            conn.Close();
            return permissionlevel;
        }
0
Comment
Question by:cookiejar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39647150
Check that you are passing the correct number of backslashes in your username. It is difficult to tell how many slashes you have in the actual value (since backslash is the escape character in C# strings).
0
 

Author Comment

by:cookiejar
ID: 39648615
There are two backslashes.  If I remove the where clause, it works fine.
From researching on line, I tried parametizing but it still doesn't work.  

protected int getdata(string username)
        {
            int permissionlevel = 0;
                                   
            SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MyTestDbConnectionString"].ConnectionString);

               string sql = "select * from PermissionLevel where UserName= @user";
            SqlCommand sc = new SqlCommand(sql,conn);
            sc.Parameters.AddWithValue("@user", username);
            sc.CommandType = System.Data.CommandType.Text;
            conn.Open();

            SqlDataReader rdr = sc.ExecuteReader();
                   
             while (rdr.Read())
            {
                permissionlevel = rdr.GetInt32(2);            
            }
            rdr.Close();
            conn.Close();
            return permissionlevel;
        }
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question