Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SqlDataReader is not working - does not read data

Posted on 2013-11-13
2
Medium Priority
?
904 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

715 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