Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone.I have login page,which redirects user to another page on successfull login.I want to set such a functionality that,EMPLOYEE NAME AND EMPLOYEE CODE should be displayed as and when the user sees the redirected page(page that is being redirected on login).so i am passing the username field of the login page to redirected page using query string and based on that fetching database values and assigning them to textboxes. But it is not working as expected,it doesnt displays name and code on page load...my code is below...any help...would be greatly appriciated...

C#
protected void Page_Load(object sender, EventArgs e)
        {
            
            string str = Request.QueryString["uname"];
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select name,code from emp_details where uname='" + str + "'", conn);
          
            SqlDataReader dr = cmd.ExecuteReader();
            
                name.Text = dr["name"].ToString();
                code.Text = dr["code"].ToString();
           

        }
Posted
Updated 20-Jan-13 20:36pm
v3

use this
C#
protected void Page_Load(object sender, EventArgs e)
{

string str = Request.QueryString["uname"];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select name,code from emp_details where uname='" + str + "'", conn);
DataSet ds=new DataSet();
da.fill(ds);
name.Text = ds.Tables[0].Rows[0]["name"].ToString();
code.Text = ds.Tables[0].Rows[0]["code"].ToString();

 
}
 
Share this answer
 
v2
Comments
Thanks7872 21-Jan-13 1:26am    
da.fill(ds) gives me the error that**** 'System.Data.SqlClient.SqlDataAdapter' does not contain a definition for 'fill' and no extension method 'fill' accepting a first argument of type 'System.Data.SqlClient.SqlDataAdapter' could be found (are you missing a using directive or an assembly reference?***** any help?
AdityaPratapSingh 21-Jan-13 1:31am    
Use System.Data;
Use System.Data.SqlClient;

and update your code
da.fill(ds); to da.Fill(ds);

if you still getting error let me know
Thanks7872 21-Jan-13 1:36am    
oh...so silly of me....it was 'f'ill instead of 'F'ill....i am passing xyz@domain.us as a username,right.this gives me the error at da.Fill(ds) that incorrect syntax near xyz@domain.us...why this is so?whats wrong with that?not able to figure it out?
AdityaPratapSingh 21-Jan-13 1:38am    
pass it as string
Thanks7872 21-Jan-13 1:42am    
am not getting what you mean as m novice...can you plz write me the line of example....?
Hello

Your code not seem wrong But you take some filter
like first check your query string is not null or empty or other format

after that pass it as Sql Parameter using Parameter.AddwithValue


And Last Check your Reader HasValue or not


After That you get What Problem Your Code Have?
Then Assign Its Value into Your TextBox..


I Hope Your Problem Will Solve IF Not Please give your Comment,,
 
Share this answer
 
Comments
Thanks7872 21-Jan-13 4:36am    
the last try that is remaining is cmd.parameters.addwithvalue...but m not familiar with it...how to do that?my db clolomn name is UNAMe and the string is str,so m using select******from*****where uname='"+str+"'...how to apply this?
rizwan muhammed khan gouri 21-Jan-13 5:06am    
SqlCommand cmd = new SqlCommand("select name,code from emp_details where uname=@str, conn);
cmd.Parameters.AddWithValue("@str", str);
I assume this is ASP.NET although you did not say that ? What do you mean 'not working as expected' ? This code is horrendous, I can use this page to erase your DB due to SQL injection. However, this code will keep reading row after row and setting the text over and over. If your last two is blank, then the values will be blank. Do you understand this code ?
 
Share this answer
 
Comments
Thanks7872 21-Jan-13 0:10am    
I understand the code.what does it mean by 'not working as expected' that i have already explained in my question.Security is not my concern,the problem is that i should have the values displayed in the textboxes on pageload,currently textboxes remains blank on pageload.do you have any idea regarding this? further,what would you suggest as to overcome this issue?
Christian Graus 21-Jan-13 0:45am    
I have told you the answer. If you understood the code, you'd see the answer is obvious. You wrote a loop that doesnt ADD to the text of the two text boxes, it sets them over and over again. So if your sql returns more than one set of values, only the last ones are shown, and if the last one is blank, they will be blank. Do you know how to use a debugger ?
Thanks7872 21-Jan-13 2:27am    
oh man.....let us remove that whole loop...than?refer to the updated que. above.i am getting error before the loop itself,so no issue regarding loop....we are here to solve errors and help others,so if possible provide corrections not just arguements.....
Christian Graus 21-Jan-13 3:07am    
So the issue is that you got an error, didn't tell us you had an error, let alone what it was, and now it's my fault for trying to work my way through the web of your poorly asked question ? Sorry, I tried my best, but you just were not clear at all.

The loop is still retarded. It's clear you had no idea what it did, the code you wrote makes zero sense.
HI,

1St you check the data for number of rows returning from the database and then use to assign the values.

protected void Page_Load(object sender, EventArgs e)
{
 
string str = Request.QueryString["uname"];
SqlConnection conn = new SqlConnection();
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
conn.Open();
SqlDataAdapter da=new SqlDataAdapter("select name,code from emp_details where uname='" + str + "'", conn);
DataSet ds=new DataSet();
da.fill(ds);
if(ds.Tables[0].Rows.Count == 1)
{
name.Text = ds.Tables[0].Rows[0]["name"].ToString();
code.Text = ds.Tables[0].Rows[0]["code"].ToString();
 }

else
{
name.Text = "Multiplevalues";
}
 
}


Check which value you are getting in your textbox.

Thanks
 
Share this answer
 
 
Share this answer
 
Have you tried with if(dr.Read())?
C#
protected void Page_Load(object sender, EventArgs e)
        {
            
            string str = Request.QueryString["uname"];
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select name,code from emp_details where uname='" + str + "'", conn);
          
            SqlDataReader dr = cmd.ExecuteReader();
                if(dr.Read()) // Checks if there is value to read.
                {
                   name.Text = dr["name"].ToString();
                   code.Text = dr["code"].ToString();
                }
           
 
        }

There is possibility that you won't have any value against that string in DB.
Check by placing breakpoint on if.
Also don't forget to close dr after you finish your operation by writing:
C#
if(dr!=null)
dr.Close();
 
Share this answer
 
Comments
Thanks7872 21-Jan-13 4:33am    
sir,i already checked there is value against that string in db.further,m getting error before dr...so no issue regarding dr.close etc......have you gone through the above code....it seems ok to me since last 4 hours,but problem is that at the line

SqlDataReader dr = cmd.ExecuteReader();

shows error....means cmd is getting NO VALUES.....Why?that is what i am wondering about?
Kishor Deshpande 21-Jan-13 4:42am    
http://stackoverflow.com/questions/11225127/no-value-given-for-one-or-more-parameters-with-c-sharp

Refer above url, it seems name or code is not the column name in table emp_details at DB side which you have written in command query.

You get NO VALUES error when column name at DB and query column doesn't match.

Got it I guess: SELECT UNAME, CODE instead of SELECT NAME, CODE

If yo still face issue, copy and paste error message, I'll help you.
Kishor Deshpande 21-Jan-13 4:49am    
Try with this command:
SqlCommand cmd = new SqlCommand("select uname,code from emp_details where uname='" + str + "'", conn);
Thanks7872 21-Jan-13 5:16am    
let me explain....i have coloms name,code,uname,pswd in db....i am passing uname that is username,from login page to redirected page and storing it as string in str(using it in query).further,i want name and code field to be displayed on page load,so i am retrieving name,code using str so my query is

SqlCommand cmd = new SqlCommand("select name,code from emp_details where uname='" + str + "'", conn);

at this stage 'str' has value 'xyz@domain.us'

then i wrote

sqldatareader dr=cmd.executereader();

at this line i got the error that INCORRECT SYNTAX NEAR XYZ@DOMAIN.US....
Kishor Deshpande 21-Jan-13 5:23am    
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "select name,code from emp_details where uname = @uname";

cmd.Connection=conn; cmd.Parameters.Add(new SqlParameter("@uname",str));
conn.Open();

SqlDataReader dr = cmd.ExecuteReader();

if(dr.Read()) // Checks if there is value to read.

{ name.Text = dr["name"].ToString();

code.Text = dr["code"].ToString();
}

Then might be Verbatim string issue, try above code..

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