locked
Get record in webservice. RRS feed

  • Question

  • User1734272365 posted

    Hello everyone !

    I have trouble with webservice, I have code 

        [WebMethod]
        public string ten_phanmem(string PassGiaiMa)
        {
            ds = new DataSet();
            string strConnection = ConfigurationManager.ConnectionStrings["connectring1"].ConnectionString;
            conn = new SqlConnection(strConnection);
            SqlCommand cmd = conn.CreateCommand();
    
            try
            {
                da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa =+PassGiaiMa+", conn);
                da.Fill(ds);
                //return ds;
            }
            catch (SqlException)
            {
            }
            finally
            {
                conn.Close();
            }
        }

    How do insert string PassGiaiMa in da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa =+PassGiaiMa+", conn);

    And how do return this value to xml ?

    Wednesday, July 16, 2014 3:17 AM

Answers

  • User-417640953 posted

    Hi kimlong008,

    Thanks for your post.

    kimlong008

    da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa =+PassGiaiMa+", conn);

    As for the sqlcommand string you can write it like below.

    da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa ='"+PassGiaiMa+"'", conn);

    And the response data format based on your client request's header dataType.

    If you use ajax call the web service, you can set the datatype as "application/xml".

    $.ajax({
       type: 'GET',
       url: 'http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles',
       contentType: 'application/json; charset=utf-8',
       dataType: 'application/xml',
       data: '{"JSONUserCode":"1234"}',
       success: myCallback
    });

    If you use the HttpWebRequest in server side code you can set the Accept header information like below.

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles");
     request.Accept = "application/soap+xml";

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 5:22 AM

All replies

  • User-760709272 posted

    Put the text in the SqlCommand and use a parameter to supply the data.  Example on this page

    http://www.codeproject.com/Articles/361579/A-Beginners-Tutorial-for-Understanding-ADO-NET

    Wednesday, July 16, 2014 4:32 AM
  • User768703680 posted

    Hi,

    Try to implement the code in Stored Procedure but not in lnline text as it tends to SQL Injection Attacks. Implement the stored procedure in DAL by passing the parameter as value in stored procedure and calling the select statement in SqlDataAdapter as

    using(SqlDataAdapter da=new SqlDataAdapter())

    {

    da.SelectCommand=cmd;

    }

     

    Thursday, July 17, 2014 2:23 AM
  • User-417640953 posted

    Hi kimlong008,

    Thanks for your post.

    kimlong008

    da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa =+PassGiaiMa+", conn);

    As for the sqlcommand string you can write it like below.

    da = new SqlDataAdapter("SELECT * FROM [services]  where PassGiaiMa ='"+PassGiaiMa+"'", conn);

    And the response data format based on your client request's header dataType.

    If you use ajax call the web service, you can set the datatype as "application/xml".

    $.ajax({
       type: 'GET',
       url: 'http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles',
       contentType: 'application/json; charset=utf-8',
       dataType: 'application/xml',
       data: '{"JSONUserCode":"1234"}',
       success: myCallback
    });

    If you use the HttpWebRequest in server side code you can set the Accept header information like below.

     HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dev.formshare.gologictech.com/JSON/JSONService.asmx/GetUserRoles");
     request.Accept = "application/soap+xml";

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 17, 2014 5:22 AM