Click here to Skip to main content
15,895,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi again
i want to write method that get IDnumber from textBox,then
SQL
select name,family from PersonelTable where personId=textBoxID.text

then return name and family so i can insert them to another table.
please help.
if you have another easy way to do this,please tell me.

thanks
Posted
Comments
zyck 12-Feb-12 9:09am    
where do you get IDnumber? from DB? or user will provide the IDnumber then input to the txtBoxID?



You can use this code reference below

C#
/*Property or Data Transfer Objects*/
public class Employee {

    public int empId { get; set; }
    public string employee { get; set; }

}


/* Method */
public List<employee> GetEmployeeByID(int empId) {

    var result = from e in Employees
                 where e.EmpID == empId
                 select new { e.FirstName, e.LastName };

    return result.List();
}

if(this.textBoxID.text != string.Empty){

    /*Use the Method*/
    List<employee> ListResult = GetEmployeeByID(this.textBoxID.text);

    /*Bind to Gridview*/
    MyGrid.DataSource = ListResult;
    MyGrid.DataBind

}


vote and accept solution
If this will help you
thanks
 
Share this answer
 
v2
Comments
NeptuneHACK! 13-Feb-12 15:37pm    
my 5
Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT name, family FROM PersonelTable where personId=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", textBoxID.Text);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            if (reader.Read())
                {
                string name = (string) reader["name"];
                string family = (string) reader["family"];
                ...
                }
            }
        }
    }
 
Share this answer
 
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling;
public static Employee GetEmployeeByID(Int32 personID)
{
Employee objEmployee = new Employee();
Database objDatabase = DatabaseFactory.CreateDatabase();
DbCommand objDbCommand = objDatabase.GetStoredProcCommand("SP_Name");
try
{
objDatabase.AddInParameter(objDbCommand, "@PersonID", DbType.Int32, personID);
using (IDataReader dr = objDatabase.ExecuteReader(objDbCommand))
{
while (dr.Read())
{
BuildModel(dr, objEmployee );
}
}
}
catch (DbException ex)
{
bool rethrow = ExceptionPolicy.HandleException(ex, "Data Context Exception Policy");
if (rethrow)
throw;
}
return objEmployee ;
}
C#
private static void BuildModel(IDataReader objDataReader, Employee objEmployee)
        {
            DataTable objDataTable = objDataReader.GetSchemaTable();

            foreach (DataRow dr in objDataTable.Rows)
            {
                string column = dr.ItemArray[0].ToString();
                switch (column)
                {
                    case "Name":
                        objGroupInfo.Name=objDataReader["Name"];
                        break;
                    case "Family":
                        objGroupInfo.Family=objDataReader["Family"];
                        break;
               }
          }
}
 
Share this answer
 

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