Ibrahim Shaikh Ibrahim Shaikh - 1 month ago 21
ASP.NET (C#) Question

Missing parameter in web service C#

I am creating a web service in C#:

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void upd(string id, string upddate,string pm)
{
SqlCommand cmd = new SqlCommand("updcsuv", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;

SqlParameter[] param =
{
new SqlParameter("@id",id),
new SqlParameter("@upddate",upddate),
new SqlParameter("@username",pm)

};
cmd.Parameters.AddRange(param);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}


and the stored procedure for the web service is

CREATE proc updcsuv
(@id int,
@upddate datetime,
@username varchar(30))
as
begin
update csuv
set date = @upddate,
UserName = @username,
where
id = @id
end


I am getting the error in my web service and I don't understand why the error is happening as I think my web service is correct and I passed all the parameters correctly

This is the error


System.InvalidOperationException: Missing parameter: upddate.

at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)

at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)

at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()

at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()

Answer

Like you said your problem line is new SqlParameter("@upddate",upddate). So you are specified in your query params that upddate is datetime type. But you are giving string to it. So this can create a problem, also I suppose your string is empty and you forgot to add your DateTime which you create before that.

if (upddate.ToString() == "")
{
    dt = Convert.ToDateTime("01-01-1900", enGB);
}
else
{
    dt = Convert.ToDateTime(date, enGB);
}

//when you go to the params, set dt for upddate instead of your string.
SqlParameter[] param =
{
    new SqlParameter("@id",id),
    new SqlParameter("@upddate",dt),
    ....
}

EDIT

If you want the update date, you should just write is like this:

if (upddate.ToString() == "")
{
    dt = DateTime.Now;-> this will return you current date, which will be the update date !
}