简体   繁体   中英

How to call update function of web service from web application

I have a web service function which is to update my database. I would like to call it from my web application but it won't work. It can be update through my web service but not my web application after I have call my web service. Is my code of calling the service function correct?

Here is my web service update function :

[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!";
}

This is my web application code that call the function:

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)));
}

}

In my sqlcommand I try to remove the WHERE, my web application can be update data to database, but for all row the data updated are all the same. But when I put back there WHERE statement follow with Session["username"], my update in web application won't store any data. Any help with this? I really have no idea how to update my data using session.

Add your web service in to your service reference.

It will generate a client.Create a client for your service. And you can access the method through your client.

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

您可以执行的操作是添加一个try catch,以查看是否可以捕获该异常,如果有错误,则返回一个字符串。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM