Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have one textbox which is ID and another as EMPNAME .so when i write the particular id in the text box it should display the EMPNAME in the other text box .
how shall i do this.
Posted
Comments
bhagirathimfs 25-May-12 6:57am    
you have stored the empname and id in a table(in a database)???
Member 8388026 25-May-12 7:38am    
yes

the query will be like this:

C#
cmd = sqlcommand(select EMPNAME from table_Name where ID = "  + TextBox1.Text + "   ,con)


Then
turn autopostback of the textbox to true

Code:

C#
protected void TextBox1_TextChanged(object sender, System.EventArgs e)
{
    txtEMPNAME.Text = <call your="" function="" where="" you="" have="" written="" the="" query=""> ;
}</call>
 
Share this answer
 
v2
Comments
Member 8388026 25-May-12 8:08am    
can u tell me any website for this demo
bhagirathimfs 25-May-12 8:10am    
Nice answer 5!
C#
string ConStr = @"server="serverName";database=dbName;uid=username;pwd=password";
            DataSet ds = new DataSet();
            SqlConnection Con = new SqlConnection(ConStr);


            Con.Open();

            SqlCommand cmd = new SqlCommand("Select EMPNAME from TableName Where id='"+txtid.text+"'", Con);
            txtEMPNAME.text = Convert.ToString(cmd.ExecuteScalar());
            
               
                Con.Close();
 
Share this answer
 
v2
To add to previous answers, never concatenate values directly to your SQL statements. This will leave you vulnerable to SQL injections, data type conversion mismatches and so on.

The proper way to use user input in a SQL statement is to use a SqlParameter[^]

So your code could look like:
C#
...
SqlCommand command = new SqlCommand("SELECT EmpName FROM YourTable WHERE Id = @Id", Con);
command.Parameters.AddWithValue("@Id", TheTextBox.Text);
...
 
Share this answer
 
you can do it same without post back of your page by using ajax.

C#
function GetNameById(EmpCode,EmpName)
        {
            

            var retVal = doAjax('frmGetDetail.aspx','EmpCode='+EmpCode.value,0);

            if(retVal.length>0)
            {
                EmpName.value=retval;
                return true;
            }
            else
            {
                alert('The Employee Code Is not Exist.');
                return false;
            }
                

        }


function doAjax(url,query,getxml) 
    {
        var req;
        
        try {
         req = new XMLHttpRequest(); /* e.g. Firefox */
         } catch(err1) {
           try {
           req = new ActiveXObject('Msxml2.XMLHTTP'); /* some versions IE */
           } catch (err2) {
             try {
             req = new ActiveXObject("Microsoft.XMLHTTP"); /* some versions IE */
             } catch (err3) {
              req = false;
             }
           }
         }
        req.open("POST", url + '?' + query,false);
        req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        req.send();
        if(req.readyState == 4) 
        {
           if(req.status == 200) 
           {
              var item = req.responseText;
              if(getxml==1) 
              {
                 item = req.responseXML;
              }
           }
        }
        return item;
}



in your .cs page add attribute to your textbox in which you enter id
C#
protected void Page_Load(object sender, EventArgs e)
    {

txtEmpcode.Attributes.Add("onblur", "return GetNameById(" + txtEmpcode.ClientID + "," + txtEmpName.ClientID + ")");
}



now you add a page of ajax in which you call your query to get empname:-
in this aspx page only enter page directive which i have written and no any line needed to add
ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmGetDetail.aspx.cs" Inherits="frmGetDetail" Theme=""%>


now in this page cs call your query like that

C#
protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write(getEmpNameById(Request.QueryString["EmpCode"].ToString()));
    }

private string getEmpNameById(string EmpCode)
        {
string ConStr = @"server="serverName";database=dbName;uid=username;pwd=password";
            DataSet ds = new DataSet();
            SqlConnection Con = new SqlConnection(ConStr);
 
            Con.Open();
 
            SqlCommand cmd = new SqlCommand("Select EMPNAME from TableName Where id='"+ EmpCode +"'", Con);
            ds= cmd.ExecuteDataSet());
            Con.Close();
               if (ds.Tables[0].Rows.Count > 0)
                  return(ds.Tables[0].Rows["EmpName"].ToString())
               else
                 return ""; 
                
        }
 
Share this answer
 
v3

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