Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey Friends... I'm getting this error in my code.. that my sqldatareader not reading any row from database.. though i have inserted many data in my table.

here is the Code:
C#
public partial class Welcome : System.Web.UI.Page
{

    public string username; 

    public string fname;
    public string lname;


    protected void Page_Load(object sender, EventArgs e)
    {
        bindTable2();
    }

    protected void bindTable2()
    {

        string sqlSel = "select * from Table2 where username ='" + username + "' ";

        SqlConnection con = new SqlConnection();
        con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        con.Open();

        SqlCommand cmd = new SqlCommand(sqlSel, con);
        SqlDataReader sdr = cmd.ExecuteReader();


        sdr.Read();
            

        username = sdr["username"].ToString();

                fname = sdr["fname"].ToString();

                ........... etc
                sdr.close();
          
     }
        
}

And if i'm using :
C#
if (sdr.HasRows)
        {
            while (sdr.Read())
            {

                //All Data here
            }
            sdr.Close();
        }
        else
        {
           Response.Write("I didn't find any relevant data");
          
        }
     }

Then it automatically taking my code to else statement. It's not reading any data from Table2.
don't know why.... Please help me out in it.
Posted
Updated 12-Aug-13 18:13pm
v4
Comments
[no name] 12-Aug-13 15:52pm    
Probably because your query is not returning anything.
sh4kti 12-Aug-13 15:57pm    
that's what i want to know why ??
the record is in Table2... i think i'm using the right query..
if u have any other then do tell me plz.
fyulaba 12-Aug-13 16:02pm    
Have you set a breakpoint on setting the sqlSel string and used the string as a query in SQL manager to check it should be returning rows?
sh4kti 12-Aug-13 16:10pm    
If i'm using ::

string sqlSel = "select top 1 * from Table2 order by Id desc";

then it displaying the last entered data...
by my this query is not running when selecting from username.
idenizeni 12-Aug-13 16:26pm    
Where is the code where you set the username variable? The issue is probably because you never set the username variable so the query is looking for the record with a blank username and I am guessing there is no SQL record with a blank username. If you do as fyulaba suggests and set a breakpoint at the line after you set the sqlSel value you can see the actual SQL query going to the SQL server.

Where are you initialising te username variable? if its blank your sql query might not return any rows.
 
Share this answer
 
Comments
sh4kti 12-Aug-13 15:58pm    
username variable in in table1 and in table2
i want to fetch the record from username which i have inserted into table2.
fyulaba 12-Aug-13 16:07pm    
First line of bindTable2 you are reading from the username variable but it may not have been set yet.

string sqlSel = "select * from Table2 where username ='" + username + "' ";
sh4kti 12-Aug-13 16:15pm    
Yeah that's where i too stuck... but don't understand what to do as i have to display the data with username.

if i use ::

string sqlSel = "select top 1 * from Table2 order by Id desc";

then it displaying the last entered data...

but i need the query to fetch the data by username.
Richard C Bishop 12-Aug-13 16:52pm    
Use the "Have a Question of Comment?" widget to ask questions. You posted a question as a solution to this question. Do you see the paradox you created?
sh4kti 12-Aug-13 17:22pm    
am sorry... from next time i will use the reply option.
Maybe the query isn't sending data back because the variable username has no value yet.

Check in your DB manager if that query returns the desired data.
 
Share this answer
 
v2
Comments
sh4kti 12-Aug-13 16:31pm    
I have made a login and register form... in which i had taken two tables table1 and table 2.
i'm using username as a foreign key in table2 ..
it's a 2 step process or filling the Registration form.
the Registration page is working fine.. and username automatically get filled in table2..it working correctly in both the table.

now i want to display the profile of user.. where i'm using this Query to get the data to be displayed.

rest of mine code is working fine.. but here only i get stuck.

am sorry i didn't understand what you are trying to say.. " assign a value to string variable username ??? ""
in aspz.cs
i'm using like this

Username : <%=username %>
.....
NOW TELL HELP ME..
Richard C Bishop 12-Aug-13 16:52pm    
Use the "Have a Question of Comment?" widget to ask questions. You posted a question as a solution to this question. Do you see the paradox you created?
silvercr0w 12-Aug-13 18:03pm    
I'm sorry, thanks for the advice richcb. I will edit my previous answer.
Richard C Bishop 13-Aug-13 10:07am    
It's all good, just a friendly reminder.
string sqlSel = "select * from Table2 where username ='" + username + "' "; have you make a breakpoint to see the username's value in this code ?

I think username hasn't got a value everytime the page is loaded
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900