2013-10-24 109 views
0

我想學習C#並遇到問題,但我不確定它是什麼。我正嘗試通過Web服務從實體框架調用存儲過程。然而,我不斷收到一個錯誤:我的SQL數據表只是一個基本的表(在其他情況下工作正常)。我已經將存儲過程(CreateUser)放入我的實體數據模型中。然後我創建了一個沒有返回值的函數(CreateUser)。將類添加到只有1個參數的方法中?

這是函數,在我WebService.cs,我試圖通過爲我找回了上述錯誤:

public void AddUser(string Name, int Contact, string Email, string Password, bool Admin, string ImageURL) 
    { 
     UsersEntities entdb = new UsersEntities(); 

     NewUser newuser = new NewUser { 
         Name = Name, 
         ContactNo = Contact, 
         Email = Email, 
         Password = Password, 
         Admin = Admin, 
         ImageURL = ImageURL}; 
     entdb.CreateUser(newuser); 
    } 

由於先前的錯誤,我也創建了一個類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

class NewUser 
{ 
    public string Name { get; set; } 
    public int ContactNo { get; set; } 
    public string Email { get; set; } 
    public string Password { get; set; } 
    public bool Admin { get; set; } 
    public string ImageURL { get; set; } 

} 

而且從正常的頁面調用Web服務(我只是想測試它,所以我沒有把任何其它的值):

protected void AddBtn_Click(object sender, EventArgs e) 
{ 
    CustomWebService cws = new CustomWebService(); 
    cws.AddUser("testName", 12345678, "testEmail", "testPassword", true, "testImage"); 
} 

如果有人能幫助,我將不勝感激!

在此先感謝!

編輯:添加CREATEUSER功能:

 #region Function Imports 

    /// <summary> 
    /// No Metadata Documentation available. 
    /// </summary> 
    /// <param name="name">No Metadata Documentation available.</param> 
    /// <param name="contactNo">No Metadata Documentation available.</param> 
    /// <param name="email">No Metadata Documentation available.</param> 
    /// <param name="password">No Metadata Documentation available.</param> 
    /// <param name="admin">No Metadata Documentation available.</param> 
    /// <param name="imageURL">No Metadata Documentation available.</param> 
    public int CreateUser(global::System.String name, Nullable<global::System.Int32> contactNo, global::System.String email, global::System.String password, Nullable<global::System.Boolean> admin, global::System.String imageURL) 
    { 
     ObjectParameter nameParameter; 
     if (name != null) 
     { 
      nameParameter = new ObjectParameter("Name", name); 
     } 
     else 
     { 
      nameParameter = new ObjectParameter("Name", typeof(global::System.String)); 
     } 

     ObjectParameter contactNoParameter; 
     if (contactNo.HasValue) 
     { 
      contactNoParameter = new ObjectParameter("ContactNo", contactNo); 
     } 
     else 
     { 
      contactNoParameter = new ObjectParameter("ContactNo", typeof(global::System.Int32)); 
     } 

     ObjectParameter emailParameter; 
     if (email != null) 
     { 
      emailParameter = new ObjectParameter("Email", email); 
     } 
     else 
     { 
      emailParameter = new ObjectParameter("Email", typeof(global::System.String)); 
     } 

     ObjectParameter passwordParameter; 
     if (password != null) 
     { 
      passwordParameter = new ObjectParameter("Password", password); 
     } 
     else 
     { 
      passwordParameter = new ObjectParameter("Password", typeof(global::System.String)); 
     } 

     ObjectParameter adminParameter; 
     if (admin.HasValue) 
     { 
      adminParameter = new ObjectParameter("Admin", admin); 
     } 
     else 
     { 
      adminParameter = new ObjectParameter("Admin", typeof(global::System.Boolean)); 
     } 

     ObjectParameter imageURLParameter; 
     if (imageURL != null) 
     { 
      imageURLParameter = new ObjectParameter("ImageURL", imageURL); 
     } 
     else 
     { 
      imageURLParameter = new ObjectParameter("ImageURL", typeof(global::System.String)); 
     } 

     return base.ExecuteFunction("CreateUser", nameParameter, contactNoParameter, emailParameter, passwordParameter, adminParameter, imageURLParameter); 
    } 
+1

顯示'UserEntities'。 –

+1

你能向我們展示'CreateUser'的方法簽名嗎? –

+0

請向我們展示您的'UsersEntities.CreateUser()'函數的代碼。它似乎並不需要你期望的參數。 –

回答

0

你CREATEUSER方法需要6個參數(名稱,contactNo,電子郵件,密碼,管理員,的ImageUrl),但在用戶,這是一個參數傳遞。你需要這樣做:

NewUser newuser = new NewUser { 
        Name = Name, 
        ContactNo = Contact, 
        Email = Email, 
        Password = Password, 
        Admin = Admin, 
        ImageURL = ImageURL}; 
    entdb.CreateUser(newuser.Name, newuser.ContactNo, newuser.Email, newuser.Password, newuser.Admin, newuser.ImageURL); 

修改方法只需要一個NEWUSER參數和方法體,使用屬性。

+0

它的工作原理!謝謝! – Jos