簡體   English   中英

如何從Web應用程序調用Web服務的更新功能

[英]How to call update function of web service from web application

我有一個Web服務功能,用於更新數據庫。 我想從我的Web應用程序中調用它,但是它不起作用。 調用我的Web服務后,可以通過我的Web服務而不是我的Web應用程序對其進行更新。 我調用服務函數的代碼正確嗎?

這是我的Web服務更新功能:

[WebMethod(EnableSession = true)]
public string sell_item(string name, string photo, string description, string initial_price, string bid_time)
{
    SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=Bidding;Integrated Security=True");
    con.Open();
    SqlCommand cmd = new SqlCommand("UPDATE login SET name = @name, photo = @photo, description = @description, initial_price = @initial_price, bid_time = @bid_time where username='"+ Session["username"] +"'", con);

    cmd.Parameters.AddWithValue("@name", name);
    cmd.Parameters.AddWithValue("@photo", photo);
    cmd.Parameters.AddWithValue("@description", description);
    cmd.Parameters.AddWithValue("@initial_price", initial_price);
    cmd.Parameters.AddWithValue("@bid_time", bid_time);

    cmd.ExecuteNonQuery();
    con.Close();
    return "Product has been upload successfully!";
}

這是調用該函數的Web應用程序代碼:

public partial class bidpage : System.Web.UI.Page
{
abc.Service a = new abc.Service();
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = Convert.ToString(a.sell_item(Convert.ToString(TextBoxProduct.Text), Convert.ToString(TextBoxPhoto.Text), Convert.ToString(TextBoxDescription.Text), Convert.ToString(TextBoxPrice.Text), Convert.ToString(TextBoxBidTime.Text)));
}

}

在我的sqlcommand中,我嘗試刪除WHERE,我的Web應用程序可以將數據更新到數據庫,但是對於所有行,更新的數據都相同。 但是,當我將WHERE語句放回會話[[username]]后,在Web應用程序中的更新將不會存儲任何數據。 有什么幫助嗎? 我真的不知道如何使用會話更新數據。

將您的Web服務添加到服務參考中。

它將生成一個客戶端。為您的服務創建一個客戶端。 您可以通過客戶訪問該方法。

Yourservice.ServiceClient client = new  Yourservice.ServiceClient();
client.SellItem();

您可以執行的操作是添加一個try catch,以查看是否可以捕獲該異常,如果有錯誤,則返回一個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM