Solved

SqlDataReader is not working - does not read data

Posted on 2013-11-13
2
793 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
2 Comments
 
LVL 74

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
import issue in bit values 3 44
Build JSON from table records 17 41
c# code 19 61
Iterate a dictionnary to change values 4 31
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now