I'm trying to pass a object from .NET MVC to Web Service (VB.NET) suing SOAP.
Passing individual fields works but when I try to pass an object, it throws an error
Cannot convert from Project.Models.Table to Project.WebService.Table
REQUIREMENTS
To pass an Object from MVC to Web Service.
Below is my Web Service code.
Questions: Do I need to serialize once I get the object.
<WebMethod()>
Public Function FormData(ByVal obj As Table)
Dim sqlconn As New SqlConnection
Dim sqlcmd As New SqlCommand
Try
Dim formSerializer As New XmlSerializer(GetType(Table))
Using reader As TextReader = New StringReader(obj)
data = formSerializer.Deserialize(reader)
End Using
Below is my MVC Controller, I have added the Service Reference.
public ActionResult Submission(Table data)
{
Table obj = new Table();
FormService.WebServiceSoapClient client = new FormService.WebServiceSoapClient();
obj = client.FormData(data);
return obj;
}
The error is while passing the data object from Controller to Web Service.
The Table model class with all the fields are added in both the projects.
Your help is appreciated.
Thanks.
UPDATE
As per the suggestions of @Panagiotis Kanavos, I tried using AutoMapper. So below is the working code. I am able to store values to database. Please do let me know if its correct and secured way. Thanks.
CLIENT SIDE
public JsonResult PostMethod(Table data)
{
FormService.WebServiceSoapClient client = new FormService.WebServiceSoapClient();
var config = new MapperConfiguration(cfg => { cfg.CreateMap<Table, Table_WS>(); });
IMapper iMapper = config.CreateMapper();
var destination = iMapper.Map<Table, Table_WS>(data);
var result = client.FormData(destination);
WEB SERVICE
<WebMethod()>
Public Function FormData(ByVal Obj As Table_WS) As Table_WS
sqlconn.ConnectionString = CONNECTION_STRING
sqlcmd.Connection = sqlconn
sqlconn.Open()
sqlcmd.CommandType = Data.CommandType.StoredProcedure
sqlcmd.CommandText = "SPR_INSERT"
sqlcmd.Parameters.AddWithValue("@NAME", IIf(IsNothing(Obj.NAME), DBNull.Value, Obj.NAME))
Thank you for helping.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…