Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I am using a data reader to read values from a column of a table in the database.
Now, i wanted to compare the values of the column with the text in the text box.
However, only value of the first record gets compared.
This is my code, see if anyone can help!

public partial class AJAXTestPage : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader reader;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        cmd = new SqlCommand("select User_Name from Registration", con);
        reader = cmd.ExecuteReader();
        
    }
    protected void btnCheck_Availablity_Click(object sender, EventArgs e)
    {
        dt.Load(reader);
        if (txtUser_Name.Text == dt.Columns["User_Name"].ToString())
        {
            lblUnavailableUser_Name.Visible = true;
            lblAvailable.Visible = false;
        }
        else
        {
            lblAvailable.Visible = true;
            lblUnavailableUser_Name.Visible = false;
        }

    }
}



Thanks in advance!!!
Posted
Comments
RakeshMeena 15-Jun-11 2:29am    
Any particular reason for downvoting? Or is it just that you got emotional with the replies!
codesharper 15-Jun-11 2:46am    
dear ahsan22e,
why dont you use conditional query....???
like...
"select User_Name from Registration where userName='"+txtUser_Name.Text+"'"
parmar_punit 17-Jun-11 1:11am    
I've improve my solution please try it, I don't know why it is down voted...
I've just make it best as the performance issue is there. In your case each and every time when page is loaded, connectivity will be taken place, if even you have a simple button on your page that submit the page rather than doing other thing...

I would add to walterhevedeich's solution.

C#
bool Exists;
foreach(DataRow row in dt.Rows)
{
        if (txtUser_Name.Text == row["User_Name"].ToString())
        {
            Exists = true;
            break;
        }
}
if(Exists)
{
        lblUnavailableUser_Name.Visible = true;
        lblAvailable.Visible = false;
}
else
{
        lblAvailable.Visible = true;
        lblUnavailableUser_Name.Visible = false;
}
 
Share this answer
 
Comments
walterhevedeich 15-Jun-11 1:32am    
My 5. Can't believe I totally missed the point. Anyway, thanks for sharing. Cheers.
parmar_punit 17-Jun-11 1:14am    
Do you not think this take more time to check whether user is exist or not...
:-)
You need to loop through all the rows on your data table if you want to compare them to your textbox. You can try something like the following:

foreach(DataRow row in dt.Rows)
{
        if (txtUser_Name.Text == row["User_Name"].ToString())
        {
            lblUnavailableUser_Name.Visible = true;
            lblAvailable.Visible = false;
        }
        else
        {
            lblAvailable.Visible = true;
            lblUnavailableUser_Name.Visible = false;
        }
}
 
Share this answer
 
Comments
CPallini 15-Jun-11 1:24am    
Why someone downvoted you? Balanced.
walterhevedeich 15-Jun-11 1:29am    
Thanks man.
Prerak Patel 15-Jun-11 1:28am    
Here in loop, you just need to check whether user exists or not. Your code would get undesired effect. You don't even break the loop when you find a record. So, only last username would be considered. This could be the reason.
Check my solution.
Sorry to say but your code is messed up. Reasons:
1. Why execute the command in different event and load the values in different event?
2. What are you trying to achieve with this line:
if (txtUser_Name.Text == dt.Columns["User_Name"].ToString()).
This will just give the expression of the column (not value).

If you are trying to check if the entered user name exists in DB or not, apply filter to your query and make it something like this:
"select User_Name from Registration where User_Name = " + txtUser_Name.Text
and use ExecuteNonQuery method in place of "ExecuteReader" as you just want to check for the existence of a value.

Hope that helps!
 
Share this answer
 
v2
You should make a loop, on the datatable rows e.g.:
C#
foreach(DataRow row in dt.Rows)
{
  if (txtUser_Name.Text == row["User_Name"].ToString())
  {
    //...
  }
  else
  {
    //...
  }
}


BTW probably the ToString() call isn't needed.
 
Share this answer
 
I think it will help you.. I've improved my solution so it is more useful to you
public partial class AJAXTestPage : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader reader;
    DataTable dt = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
           con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
           con.Open();
           cmd = new SqlCommand("select User_Name from Registration", con);
           reader = cmd.ExecuteReader();
           // do another thing with the reader which is needed first time.
           con.Close();
        }
    }
    protected void btnCheck_Availablity_Click(object sender, EventArgs e)
    {
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        con.Open();
        // here i'm considering userName is a column name of your table, so change the name as per need...
        cmd = new SqlCommand("select User_Name from Registration where userName = @userName", con);
        cmd.Parameter.AddWithValue("@userName",txtUser_Name.Text");
        con.Open();
        reader = cmd.ExecuteReader();
        if (reader.HasRows)
        {
            lblUnavailableUser_Name.Visible = true;
            lblAvailable.Visible = false;
        }
        else
        {
            lblAvailable.Visible = true;
            lblUnavailableUser_Name.Visible = false;
        }
        con.Close();
    }
}
 
Share this answer
 
v3
Comments
codesharper 15-Jun-11 2:43am    
good logic, but It's better to use select query with specified user that none has implement...
parmar_punit 15-Jun-11 2:48am    
yes it is better to use condition within the query but op asked for that so i post this..... :)
parmar_punit 18-Jun-11 1:39am    
if anyone vote it down, then please Let me know why it is down voted...
specify the reason so i can improve my knowledge....
Thanks

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