2010-11-04 93 views
1

我正在創建Web服務以獲取客戶端應用程序中的員工詳細信息。爲了實現這一點,我都遵循以下步驟:C#中的Webservice問題.net

員工propertyclass:

public class EmployeeProperty 
{ 
    #region class Members. 
    private string _employeeid; 
    private string _employeename; 
    #endregion 

    #region class property. 

    public string EmployeeID 
    { 
     get { return _employeeid; } 
     set { _employeeid = value; } 
    } 

    public string EmployeeName 
    { 
     get { return _employeename; } 
     set { _employeename = value; } 
    } 

EmployeeDAL類:

public DataSet GetEmployeeById(EmployeeProperty employee) 
{ 
    SqlConnection conn = new SqlConnection(connStr);  
    conn.Open(); 

    // building the command 
    SqlCommand oCommand = new SqlCommand("GetEmployeeById", conn); 
    oCommand.CommandType = CommandType.StoredProcedure; 

    // Parameters 
    SqlParameter paraemployeeId = new SqlParameter(); 
    paraemployeeId.ParameterName = "@employeeId"; 
    paraemployeeId.SqlDbType = SqlDbType.VarChar; 
    paraemployeeId.Size = 15; 
    paraemployeeId.Direction = ParameterDirection.Input; 
    paraemployeeId.Value = employee.EmployeeID; 
    oCommand.Parameters.Add(paraemployeeId); 

    // Adapter and DataSet 
    SqlDataAdapter oAdapter = new SqlDataAdapter(); 

    oAdapter.SelectCommand = oCommand; 
    DataSet oDataSet = new DataSet(); 

    try 
    {  
    oAdapter.Fill(oDataSet, "Employee"); 

    if (oDataSet.Tables[0].Rows.Count > 0) 
    { 
     employee.EmployeeID = oDataSet.Tables[0].Rows[0]["Emp_ID"].ToString(); 
     employee.EmployeeName = oDataSet.Tables[0].Rows[0]["Emp_Name"].ToString();   
     return oDataSet; 
    } 
    else 
    { 
     throw new ApplicationException("Business object not found."); 
    } 
    } 
    catch (Exception oException) 
    {  
    throw; 
    } 
    finally 
    { 
    conn.Close(); 
    } 
} 

WebserviceClass:

public class EmployeeWebService : System.Web.Services.WebService 
{ 

[Webmethod] 
public DataSet GetEmployeeById(EmployeeProperty employee) 
{ 
    DataSet ds = new DataSet(); 
    EmployeeDAL empdal=new EmployeeDAL(); 
    return empdal.GetEmployeeById(employee); 
} 
} 

從客戶端應用程序:

webservice .EmployeeWebService empweb = new webservice .EmployeeWebService();  

    EmployeeProperty empproperty = new EmployeeProperty();   

    string empid = Txtempid.Text; 

    empproperty.EmpID = empid; 

    empweb.GetEmployeeById(empproperty); 

    Txtempid.Text = empproperty.EmployeeID; 

    Txtempname.Text = empproperty.EmployeeName; 

    ....like this other values should display…..but not displaying the employee name in the text box its showing empty value... 

    What i done is correct????pls help its not working...how can i achieve it. 

回答

2

您沒有使用由WS返回的DataSet

empweb.GetEmployeeById(empproperty); 

上述行返回一個包含查詢數據的DataSet

+0

好吧你的意思是說ds = empweb.GetEmployeeById(empproperty);那麼如何填充employeeproperty類成員here..for例如顯示empname txtempname.text = ds.EmpName ....我怎麼能這樣做? – sain 2010-11-04 09:31:02

+0

你將不得不爲此編寫代碼。 – leppie 2010-11-04 09:48:30

+0

dataset.tables(「tablename」)。rows(0).item(「FieldName」)。tostring – PradeepGB 2010-11-04 09:49:28