Click here to Skip to main content
15,914,322 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a TextBox TextChanged firing but when I enter a different Email Address and tab or click to the next textbox to fire the change nothing happens. I ran the program again and nothing happens. Why is this happening? Did I do something wrong? I even refreshed the page and entered in a different Email Address and the samething happened. Can someone help me with this?

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Security;
using System.Security.Cryptography;

public partial class SubmitPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       if (IsPostBack)
        {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();
            string cmdStr = "Select count(*) from TableSecurity where EmailAddress='" + TextBoxEA.Text + "'";
            SqlCommand userExist = new SqlCommand(cmdStr, con);
            SqlCommand cmd = new SqlCommand("select INST_ID, EmailAddress from TableSecurity", con);
            int temp = Convert.ToInt32(userExist.ExecuteScalar().ToString());
            if (temp == 1)
            {
                lblMessage.Text = "User Name Already Exist!!!";
            }
        }
    }

    protected void Submit_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "Select INST_ID, accessLevel, EmailAddress from TableCEO where EmailAddress='" + TextBoxEA.Text + "'";
        string cmdStr2 = "Select INST_ID, accessLevel, EmailAddress from TableIALO where EmailAddress='" + TextBoxEA.Text + "'";
        string insCmd = "Insert into TableSecurity (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        string insCmd2 = "Insert into TableSecurity (EmailAddress, Password, INST_ID, accessLevel) values (@EmailAddress, @Password, @INST_ID, @accessLevel)";
        SqlCommand insertUser = new SqlCommand(insCmd, con);
        SqlCommand insertUser2 = new SqlCommand(insCmd2, con);
        insertUser.Parameters.AddWithValue("@EmailAddress", TextBoxEA.Text);
        insertUser.Parameters.AddWithValue("@Password", TextBoxPW.Text);
        insertUser.Parameters.AddWithValue("@INST_ID", TextBoxINST_ID.Text);
        insertUser.Parameters.AddWithValue("@accessLevel", TextBoxaccessLevel.Text);
        
        try
        {
            insertUser.ExecuteNonQuery();
            con.Close();
            Response.Redirect("Login.aspx");
        }
        catch (Exception er)
        {
            lblMessage.Text = "User Already Exist";
        }
        finally
        {
        }
    }

    protected void TextBoxEA_TextChanged(object sender, EventArgs e)
    {
            SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString);
            con.Open();
            
            SqlCommand scmd = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableCEO where EmailAddress = '" + TextBoxEA.Text + "'", con);
            SqlCommand scmd2 = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableIALO where EmailAddress = '" + TextBoxEA.Text + "'", con);
            SqlDataReader dr = scmd.ExecuteReader();
            SqlDataReader dr2 = scmd.ExecuteReader();

            if (dr.Read())
            if (dr2.Read())
            
                {
                    TextBoxINST_ID.Text = dr["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr["accessLevel"].ToString();
                    TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
                    TextBoxaccessLevel.Text = dr2["accessLevel"].ToString();
                    
                }
            dr.Close();
            dr2.Close();
            con.Close();
        }
    }


It fires for the first connection but not the second connection. Why is that?
Posted
Updated 30-Oct-13 3:56am
v4

1 solution

In your textchanged event, you are using "scmd" instead of scmd2 for your dr2 SqlDataReader. So your first Select statement is getting called twice.


[EDIT]

I altered your code, try something like this:
protected void TextBoxEA_TextChanged(object sender, EventArgs e)
  {
          using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PasswordConnectionString"].ConnectionString))
          {
              con.Open();

          SqlCommand scmd = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableCEO where EmailAddress = @TextBoxEA", con);
          SqlCommand scmd2 = new SqlCommand("Select INST_ID, EmailAddress, accessLevel from TableIALO where EmailAddress = @TextBoxEA", con);

          scmd.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));
          scmd2.Parameters.Add(new SqlParameter("@TextBoxEA", TextBoxEA.Text));

          using (SqlDataReader dr = scmd.ExecuteReader())
          {
              while(dr.Read())
              {
                 TextBoxINST_ID.Text = dr["INST_ID"].ToString();
                 TextBoxaccessLevel.Text = dr["accessLevel"].ToString();
              }
          }

          using (SqlDataReader dr2 = scmd2.ExecuteReader())
          {
              while(dr2.Read())
              {
                 TextBoxINST_ID.Text = dr2["INST_ID"].ToString();
                 TextBoxaccessLevel.Text = dr2["accessLevel"].ToString();
              }
          }

        }
      }
 
Share this answer
 
v4
Comments
Computer Wiz99 30-Oct-13 10:09am    
richcb, I have fixed the issue that you pointed out but now I have an error:
SQLDataReader dr2 = scmd2.ExecuteReader();
"There is already an open DataReader associated with this Command which must be closed first."
Richard C Bishop 30-Oct-13 10:24am    
See the updated solution. I believe this should do the trick.
Computer Wiz99 30-Oct-13 11:21am    
It works. Thanks! I was just testing it out myself. I deleted the first connection and ran the program. It fired for the second one then I add the code back and it only worked for that as well. Now this is better. Thanks!!! :)
Richard C Bishop 30-Oct-13 11:26am    
You are welcome!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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