Click here to Skip to main content
15,908,264 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys.

I have 3 text boxes. When I input an ID on the first text box and matches that ID in the Database, the two remaining text boxes will become enabled. If not, an error message will display. How to do that?
Posted

1. Default status of the two text boxes define as disables
2. When inter ID in first text box match the values with your id
C#
SqlDataReader reader = null;
SqlConnection conn = new SqlConnection("Yourconnectionstring");
    conn.Open();
    SqlCommand cmd = new SqlCommand("select * from yourtable where your_id=@URID", conn);
cmd.Parameters.AddWithValue("@URID",TextBox.Text);
   reader = cmd.ExecuteReader();
    if(reader!=null && reader.HasRows){
    //Enable TextBox2 and 3
    }
 
Share this answer
 
v2
Comments
Gihan Liyanage 29-Aug-14 3:00am    
TextBox.Enabled = true;
DarkDreamer08 29-Aug-14 3:04am    
Cant I just put the code to make the two text boxes enabled inside an event on the first text box?
Gihan Liyanage 29-Aug-14 3:06am    
I dont have VS to test this code now, Any how you may allow it. And put and see what happen
DarkDreamer08 29-Aug-14 3:08am    
I tried it inside the _TextChange event. But it does not happen.
 
Share this answer
 
Assuming you use asp:TextBox:
1. add OnTextChanged="txtID_changed" in your markup
2. on the server add protected void txtID_changed(object sender, eventargs e) handler for your check
3. within the handler go to the database and validate your ID
4. if ID matches, txt2.Enabled = true; txt3.Enabled = true; else lblInfo.Text = "Error message" - lblInfo is the control you use to display messages to the user.

Assuming you use jQuery / Ajax
1. add onblur="retutn is_ID_valid(); in your input tag
2. add in your javascript tag function is_ID_valid()
3. check up the ID (jQuery.ajax call), if it matches add $("#txt2", "#txt3").attr("disabled", "false"); else show message to the user.
 
Share this answer
 
v3
Comments
DarkDreamer08 29-Aug-14 3:16am    
I tried the one you mentioned above. But nothing happens. Here is my code about that:
protected void systemid_TextChanged(object sender, EventArgs e)
{
s = WebConfigurationManager.ConnectionStrings["amicassa"].ConnectionString;
amicasaCon = new SqlConnection(s);
amicasaCon.Open();

SqlCommand cmd = new SqlCommand("SELECT system_id FROM users WHERE username = '"+username.Text+"'", amicasaCon);
SqlDataReader rd = cmd.ExecuteReader();
while(rd.Read())
{
a = rd[0].ToString();
if(a == systemid.Text)
{
password.Enabled = true;
repass.Enabled = true;
ImageButton1.Enabled = true;
}
}
rd.Close();
amicasaCon.Close();
}
Sinisa Hajnal 29-Aug-14 3:22am    
What do you mean nothing happens? does the code execute? do you get id from the database? is systemid.Text empty?

you lack FROM in the select above. Also I strongly suggest you google sql injection attacks and transfer your query into stored procedure or at least httpUtility.Encode(username.Text).

Never trust user input.
What would happen if I entered username: '; DROP TABLE users; '
first apostrophe would close your query, then drop table would execute, second apostrophe would close your own apostrophe.

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