Solved

SqlDataReader is not working - does not read data

Posted on 2013-11-13
2
873 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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

623 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