2014-04-16 27 views
0

我正在創建一個登錄頁面,用戶名和密碼應該從數據庫中檢查。 我有一個表LoginTable用戶名密碼無法通過WCF服務訪問Silverlight中的數據庫?

我的代碼如下所示

WCF服務 - LoginService.cvs.cs

public class LoginService 
{ 
    [OperationContract] 
    public int ValidateUsers(string username, string password) 
    { 
     int count; 
     string connection = ("Data Source=(localdb);Initial Catalog=smsdb; 
     Integrated Security=True;Connect Timeout=30;Encrypt=False; 
     TrustServerCertificate=False"); 
     SqlConnection conn = new SqlConnection(connection); 
     conn.Open(); 
     SqlCommand comm = new SqlCommand("[Login_Authentication]", conn); 
     SqlParameter para = new SqlParameter("@username", username); 
     comm.Parameters.Add(para); 
     SqlParameter para1 = new SqlParameter("@password", password); 
     comm.Parameters.Add(para1); 
     comm.CommandType = CommandType.StoredProcedure; 
     count = (int)comm.ExecuteScalar(); 
     return count; 
     conn.Close(); 
    } 

    // Add more operations here and mark them with [OperationContract] 
}  

和我XAML .CS

private void Button_Click(object sender, RoutedEventArgs e) 
    { 
     if (uname.Text == "" && pass.Password == "") 
     { 
      MessageBox.Show("Enter Username and password"); 
     } 
     else 
     { 
      var obj = new MyLoginService.LoginServiceClient(); 
      obj.ValidateUsersCompleted += new EventHandler 
      <ValidateUsersCompletedEventArgs>(obj_ValidateUsersCompleted); 
      obj.ValidateUsersAsync(uname.Text, pass.Password); 
     } 
    } 
    public void obj_ValidateUsersCompleted 
    (object sender, slwcftut.MyLoginService.ValidateUsersCompletedEventArgs e) 
    { 
     try 
     { 
      if (e.Result == 1) 
      { 
       MessageBox.Show("Logged in successfully"); 
      } 
      else if (e.Result <= 0) 
      { 
       MessageBox.Show("Incorrect Username or Password"); 
      } 
     } 
     catch(Exception ex) 
     { 

     } 
    } 

我沒有收到任何錯誤或答案。

+0

您是否在測試客戶端嘗試了您的服務?它工作嗎? – Sajeetharan

+0

@Sajeetharan我沒有WCF測試客戶端 – Dhinesh

+0

什麼?它帶有Visual Studio。轉到Visual Studio命令提示符並鍵入WCFTestClient – Sajeetharan

回答

1

我假設你的服務是否正常工作,

 MyLoginService.LoginServiceClient LoginClient= new MyLoginService.LoginServiceClient(); 
      { 
      LoginClient.ValidateUsersCompleted += (a, ae) => 
      { 
       if(ae.Error == null) 
       { 
       if(ae.Result != null) 
       { 
       if(ae.Result == 1) 
        { 
        MessageBox.Show("Logged in successfully"); 
        } 
        else if (ae.Result <= 0) 
        { 
        MessageBox.Show("Incorrect Username or Password"); 
        } 
       } 
       } 
       else 
       { 
        MessageBox.Show("Error occured from service"); 
       } 
      }; 
      LoginClient.ValidateUsers(uname.Text, pass.Password); 
     } 
+0

是它一個測試代碼? – Dhinesh

+0

這就是你如何致電服務 – Sajeetharan

0

您是否嘗試過使用斷點login服務的服務中,例如在

count = (int)comm.ExecuteScalar(); 

,看看它返回null,某事或什麼也看不到如果可能它連接到數據庫的錯誤,所以我會建議使用這樣的try catch語句

try 
    { 
     using (MySqlConnection conn = new MySqlConnection(constr)) 
     { 
      using (MySqlCommand cmd = new MySqlCommand()) 
      { 
       string sql = "SELECT Count(comment.Topic_Id) FROM comment inner join topic on(comment.Topic_Id=topic.id) group by topic.id limit " + start + ",10"; 
       conn.Open(); 
       cmd.Connection = conn; 
       cmd.CommandText = sql; 
       MySqlDataReader rdr = cmd.ExecuteReader(); 

       while (rdr.Read()) 
       { 
        numberOfcomments[i] = rdr.GetInt16(0); 
        i++; 
       } 
      } 
     } 
     return numberOfcomments; 
    } 
    catch(Exception ex) 
    { 

     return ex.Message; 
    } 

constr是我的字符串連接,在ex.Message中你可以看到一個字符串,說明會發生任何錯誤,希望這可以幫到你。