locked
need help in creating web service RRS feed

  • Question

  • User442781244 posted

    hi

    guys

    i have My  website build in asp.net ...that login page has..... dept....their user and pwd..

    now i have to create webservice containing

    1> list of depPartment

    2> list of user based on department

    3>also i have to make 1 webservice so that  other website  user's can send PDF file to My website's user,

    so how to acheive this.

    guys kindly suggest, if anything is not clear then plz let me know.

    Friday, November 22, 2013 2:45 AM

Answers

  • User-1509636757 posted

    Here is a bit of sample code on returning all departments:

            [WebMethod]
            public Department[] Getdepartdetails()
            {
                Department[] arrDept = null;
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select ID,DEPARTMENT_NAME,DEPARTMENT_CODE from department", con);
                    SqlDataAdapter adDept = new SqlDataAdapter(cmd);
                    DataSet dsDept = new DataSet();
                    adDept.Fill(dsDept);
                    if (dsDept != null && dsDept.Tables.Count > 0 && dsDept.Tables[0].Rows.Count > 0)
                    {
                        int iCount = 0;
                        foreach (DataRow item in dsDept.Tables[0].Rows)
                        {
                            Array.Resize(ref arrDept, iCount + 1);
                            arrDept[iCount] = new Department();
                            arrDept[iCount].DeptID = Convert.ToInt32(item["ID"]);
                            arrDept[iCount].DeptCode = Convert.ToString(item["DEPARTMENT_CODE"]);
                            arrDept[iCount].DeptName = Convert.ToString(item["DEPARTMENT_NAME"]);
                        }
                    }
                }
                return arrDept;
            }



    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 22, 2013 4:34 AM
  • User-1509636757 posted

    Have you tried to see the solution I attached? You are not looking at the whole solution. The error simply says that there is an Entity 'Department' referenced in your code which is not declared. See in App_Code folder, there should be two classes I defined Department and User.. try to convert it in vb and add in your application. It will work..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2013 12:48 AM

All replies

  • User-1509636757 posted

    Here is a complete and detailed step by step tutorial on creating and consuming web services in asp.net: Asp.net WebService or Creating and Consuming WebService in asp.net or Create and call webservice in asp.net or how to create webservice and how to use webservice in asp.net

    List down and prepare what functionality/method (or say webmethod) will required by other website and declare them in the web service..

    hope it helps./.

    Friday, November 22, 2013 3:03 AM
  • User442781244 posted

    anyone has anymore ideas???

    kindly share

    Friday, November 22, 2013 3:48 AM
  • User442781244 posted

    kaushal...

    suppose i have to make webservice  i.e list of depart....for this i have create 1 webmethod...so that other can consume it...

    so u can suggest 1. webmethod code   what i should write etc ...for this.......just for my help.

    Friday, November 22, 2013 3:56 AM
  • User-1509636757 posted

    Have you gone thru the link I shared?

    Here is a very simple but very high level overview type of illustration on how you can go on creating above three webmethods:

    right click on your project > add new item > add web service (.asmx file).. you will see that it already contains example 'Hello World'.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    
    namespace WebApplication14
    {
        /// <summary>
        /// Summary description for WebService1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
        // [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public Department[] ListDepartment()
            {
                Department[] arrDept = new Department[2];
    
                arrDept[0] = new Department();
                arrDept[0].DeptID = 1;
                arrDept[0].DeptName = "Dept 1";
    
                arrDept[1] = new Department();
                arrDept[1].DeptID = 2;
                arrDept[1].DeptName = "Dept 2";
    
                return arrDept;
            }
    
            [WebMethod]
            public User[] ListDepartment(int DeptID)
            {
                User[] arrDept = null;
                //--arrDept = Select * From User Where DeptID = @DeptID
                return arrDept;
            }
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
        }
    }

    to return pdf from webmethod you can read the pdf into bytes and return the array. Here is one nice approach discussed: http://social.msdn.microsoft.com/Forums/en-US/c75d1eb9-d682-4aaa-a591-d683cf869e5c/best-approach-to-return-pdf-through-web-service-where-pdf-is-taken-from-3rd-party-server?forum=asmxandxml

    and another one: c# - webservice to return pdf with asp.net -

    hope it helps./.

    Friday, November 22, 2013 3:59 AM
  • User442781244 posted

    hi

    kaushal..once gain thanks

    suppose webmethod for depart..then i have to take dept list in my websmethod.so webmethod would be like this

    public XXXXXXX Getdepartdetails(string department)
    {
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
    con.Open();
    SqlCommand cmd = new SqlCommand("select departname from department , con);
    //some code
    }

    this is my department table structure

    DEPARTMENT_CODE    VARCHAR2(20 BYTE)
    DEPARTMENT_NAME    VARCHAR2(100 BYTE)
    ID    NUMBER(11,0)
    LEDIT    VARCHAR2(100 BYTE)

    so what would be complete code. just give me 1 webmethod...then i will do remaining.

    Friday, November 22, 2013 4:16 AM
  • User-1509636757 posted

    Here is a bit of sample code on returning all departments:

            [WebMethod]
            public Department[] Getdepartdetails()
            {
                Department[] arrDept = null;
                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString()))
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("select ID,DEPARTMENT_NAME,DEPARTMENT_CODE from department", con);
                    SqlDataAdapter adDept = new SqlDataAdapter(cmd);
                    DataSet dsDept = new DataSet();
                    adDept.Fill(dsDept);
                    if (dsDept != null && dsDept.Tables.Count > 0 && dsDept.Tables[0].Rows.Count > 0)
                    {
                        int iCount = 0;
                        foreach (DataRow item in dsDept.Tables[0].Rows)
                        {
                            Array.Resize(ref arrDept, iCount + 1);
                            arrDept[iCount] = new Department();
                            arrDept[iCount].DeptID = Convert.ToInt32(item["ID"]);
                            arrDept[iCount].DeptCode = Convert.ToString(item["DEPARTMENT_CODE"]);
                            arrDept[iCount].DeptName = Convert.ToString(item["DEPARTMENT_NAME"]);
                        }
                    }
                }
                return arrDept;
            }



    hope it helps./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 22, 2013 4:34 AM
  • User442781244 posted

    hi kaushal

    1> list of depPartment

    2> list of user based on department...so that other can consume these

    i have to create webmethod for this i.e two webmethod:

    1> In first webmethod i have to get only department name but u ve mentioned all the fileds in th above code...????

    2>in 2nd webmethod i have to display department name with their  respective user.......

    i have 1 query i.e user will select dept name from dropdownlist..then their userlist will be displayed....

    string sql = "select * from Users where OFFICE_CODE "
                          + " IN(select OFFICE_CODE from OFFICES where DEPARTMENT_CODE IN "
                          + " (select DEPARTMENT_CODE from DEPARTMENTS1 where DEPARTMENT_NAME='" + DropDownList1.SelectedValue + "'))";

    how to fit this in 2nd webmethod

    table strcuture are as follows:

    officies:

    OFFICE_CODE    VARCHAR2(20 BYTE)
    OFFICE_NAME    VARCHAR2(70 BYTE)
    CURRENT_DIARY_NO    VARCHAR2(50 BYTE)
    FIRST_OFFICE    VARCHAR2(20 BYTE)
    SECOND_OFFICE    VARCHAR2(20 BYTE)
    THIRD_OFFICE    VARCHAR2(20 BYTE)
    FOURTH_OFFICE    VARCHAR2(20 BYTE)
    FIFTH_OFFICE    VARCHAR2(20 BYTE)
    SIXTH_OFFICE    VARCHAR2(20 BYTE)
    SEVENTH_OFFICE    VARCHAR2(20 BYTE)
    EIGHTH_OFFICE    VARCHAR2(20 BYTE)
    NINE_OFFICE    VARCHAR2(20 BYTE)
    TENTH_OFFICE    VARCHAR2(20 BYTE)
    IS_SECTION    VARCHAR2(1 BYTE)
    SECTION_CODE    VARCHAR2(20 BYTE)
    DEPARTMENT_CODE    VARCHAR2(20 BYTE)
    OFFICE_LEVEL    VARCHAR2(50 BYTE)
    TRANSACTIONS    VARCHAR2(10 BYTE)
    LETTER_CURRENT_DIARY_NO    VARCHAR2(10 BYTE)
    ID    NUMBER(11,0)
    LEDIT    VARCHAR2(100 BYTE)

    users:

    USER_CODE    VARCHAR2(20 BYTE)
    USER_NAME    VARCHAR2(60 BYTE)
    USER_DESIGNATION    VARCHAR2(60 BYTE)
    USER_PASSWORD    VARCHAR2(20 BYTE)
    OFFICE_CODE    VARCHAR2(20 BYTE)
    USER_LEVEL    CHAR(3 BYTE)
    USER_PICTURE    BLOB
    USER_DESCRIPTION    VARCHAR2(100 BYTE)
    FONT_PREFERENCE    VARCHAR2(25 BYTE)
    ID    NUMBER(11,0)

    Friday, November 22, 2013 5:11 AM
  • User-1509636757 posted

    1> In first webmethod i have to get only department name but u ve mentioned all the fileds in th above code...????

    yes, you can filter that code on whatever you want to exclude out .. can remove it. As simple as that.

    2>in 2nd webmethod i have to display department name with their  respective user.......

    Just pass the DeptName in webmethod argument and write the similar type of code which I tried to write in previous post; with this query that you have specified for fetching department of users.

    like:

            [WebMethod]
            public Department[] GetdepartdetailsByDeptName(string DeptName)

    and in query: SqlCommand cmd = new SqlCommand("select ID,DEPARTMENT_NAME,DEPARTMENT_CODE from department", con);

    replace it with whatever query you want.. and return the data accordingly..

    hope it helps./.

    Friday, November 22, 2013 5:41 AM
  • User442781244 posted

    k...kaushal..i will and getback to u if i will face nay problem

    Friday, November 22, 2013 5:44 AM
  • User-488622176 posted

    Additional remark:

    Never do this:

    string sql = "select * from Users where OFFICE_CODE "
                          + " IN(select OFFICE_CODE from OFFICES where DEPARTMENT_CODE IN "
                          + " (select DEPARTMENT_CODE from DEPARTMENTS1 where DEPARTMENT_NAME='" + DropDownList1.SelectedValue + "'))";
    

    You make yourself vulnerable to SQL injection attacks. Use the SqlCommand and SqlParameter objects to build commands and add parameters. For a code sample, see http://www.dotnetperls.com/sqlparameter

    Friday, November 22, 2013 8:01 AM
  • User-1509636757 posted

    totally agree with Illeris

    Friday, November 22, 2013 8:02 AM
  • User442781244 posted

    hi guys

    i made 1 webmethod  related to dept  as suggested in this thread.......so that other webiste user can use it.......kindly see code is correct or not..plz suggest

    [WebMethod]
        public Department[] Getdepartdetails()
        {
            Department[] arrDept = null;
            string constr;
            constr = "Provider=OraOLEDB.Oracle.1;Datasource=filetracker;User ID=filetracker;Password= filetracker";
            OleDbConnection con = new OleDbConnection(constr);
            con.Open();
            string sql = "select DEPARTMENT_NAME from department";
            OleDbCommand cmd = new OleDbCommand(sql, con);
            cmd.CommandType = CommandType.Text;
            OleDbDataAdapter da = new OleDbDataAdapter(cmd);             
            DataSet dsDept = new DataSet();
            adDept.Fill(dsDept);
            if (dsDept != null && dsDept.Tables.Count > 0 && dsDept.Tables[0].Rows.Count > 0)
                {
                    int iCount = 0;
                    foreach (DataRow item in dsDept.Tables[0].Rows)
                    {
                        Array.Resize(ref arrDept, iCount + 1);
                        arrDept[iCount] = new Department();                    
                        arrDept[iCount].DeptName = Convert.ToString(item["DEPARTMENT_NAME"]);
                    }
                }        
            return arrDept;
        }



    Saturday, November 23, 2013 12:00 AM
  • User-1509636757 posted

    Check out the sample Webservice to return list of all departments and to return list of all users based on a department: http://weblogs.asp.net/blogs/kaushal/deptWebService.zip

    hope it helps./.

    Sunday, November 24, 2013 10:05 AM
  • User-1509636757 posted

    I would also request to follow only one thread as there are too many duplicate thread on this.. just only suggestion.

    Sunday, November 24, 2013 10:17 AM
  • User442781244 posted

    kaushal

    now suggested what to do?

    i converted c# code to vb..but getting error:

    Public Function Getdepartdetails() As Department()
            Dim arrDept As Department() = Nothing
            Dim constr As String
            constr = "Provider=OraOLEDB.Oracle.1;Datasource=filetracker;UserID=filetracker;Password= filetracker"
            Dim con As New OleDbConnection(constr)
            con.Open()
            Dim sql As String = "select DEPARTMENT_NAME from department"
            Dim cmd As New OleDbCommand(sql, con)
            cmd.CommandType = CommandType.Text
            Dim adDept As New OleDbDataAdapter(cmd)
            Dim dsDept As New DataSet()
            adDept.Fill(dsDept)
            If dsDept IsNot Nothing AndAlso dsDept.Tables.Count > 0 AndAlso dsDept.Tables(0).Rows.Count > 0 Then
                Dim iCount As Integer = 0
                For Each item As DataRow In dsDept.Tables(0).Rows
                    Array.Resize(arrDept, iCount + 1)
                    arrDept(iCount) = New Department()
                    arrDept(iCount).DeptName = Convert.ToString(item("DEPARTMENT_NAME"))
                Next
            End If
            Return arrDept
        End Function

    error on these line: type department is not defined

    Public Function Getdepartdetails() As Department()
            Dim arrDept As Department() = Nothing

    arrDept(iCount) = New Department()


    Monday, November 25, 2013 12:30 AM
  • User-1509636757 posted

    Have you tried to see the solution I attached? You are not looking at the whole solution. The error simply says that there is an Entity 'Department' referenced in your code which is not declared. See in App_Code folder, there should be two classes I defined Department and User.. try to convert it in vb and add in your application. It will work..

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 25, 2013 12:48 AM
  • User442781244 posted

    kaushal  ...sugest what to do?  onething kaushal...my whole project enviornmentt is setup in virtual machine.......so i dont ve internet connectivity......this rest is also produced from virtual machine

    my connectionstring is ok.........becasue i use this connection string by connecting gridview

    constr = "Provider=OraOLEDB.Oracle.1;Datasource=filetracker;UserID=filetracker;Password= filetracker"

    when i run .asmx in broswer..i i got this:

    1>The following operations are supported. For a formal definition, please review the Service Description.

        Getdepartdetails

        HelloWorld

    2> then click on Getdepartdetails then i got this page:

    Click here for a complete list of operations.
    Getdepartdetails

    Test
    To test the operation using the HTTP POST protocol, click the 'Invoke' button.
        invoke

    3> when i clicked on inv0ke botton..this message appears:

    System.Data.OleDb.OleDbException: ORA-01017: invalid username/password; logon denied
       at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
       at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.OleDb.OleDbConnection.Open()
       at WebService.Getdepartdetails() in C:\Inetpub\wwwroot\ft\App_Code\WebService.vb:line 27

    Monday, November 25, 2013 2:13 AM
  • User-1509636757 posted

    System.Data.OleDb.OleDbException: ORA-01017: invalid username/password; logon denied

    you have some connectivity issue with oracle. check the connectionstring or contact your db admin if setup is correct related to the user you are using to connect to db. as because it is denying to logon to database.

    Monday, November 25, 2013 4:04 AM
  • User442781244 posted

    i dont ve internet connection.......but that connectionstring present in code is working fine in the case of gridview..so what is issue???

    Monday, November 25, 2013 4:13 AM
  • User442781244 posted

    kaushal.... both webmethods are displaying only 1 result....kausal need help...see code once again...

    webmethod for department name: this should display list for department name

    but i am getting only.one dept name...

    <ArrayOfDepartment><Department><DeptName> Rural Development</DeptName></Department></ArrayOfDepartment>

    plz suggest kaushal

    Monday, November 25, 2013 5:29 AM
  • User-1509636757 posted

    try debugging both the webmethods on calling

    Monday, November 25, 2013 6:43 AM
  • User442781244 posted

    both webmethoda are displaying correct result...kkkk

    buth they should dsiplay list of data...they r not displaying list..

    this means loop is not working....i.e counter is not incremented.

    kaushal see code once again.....may be some issue with inside code....

    by debugging it is sure that...u will get data in those field ......

    i have not wrritten in any code inside department.vb and user.vb. they are as it is.

    Monday, November 25, 2013 8:04 AM
  • User-1509636757 posted

    .i.e counter is not incremented.

    see!! you have your answer by yourself..counter is not incrementing.. then increment it inside loop as:

    For Each item As DataRow In dsDept.Tables(0).Rows
                    Array.Resize(arrDept, iCount + 1)
                    arrDept(iCount) = New Department()
                    arrDept(iCount).DeptName = Convert.ToString(item("DEPARTMENT_NAME"))
            iCount++;
    Next

    hope it helps./.

    Monday, November 25, 2013 8:07 AM
  • User442781244 posted

    kaushal...1 thing...in 2nd webmethod requiremnet is i.e username list should be displayed based on selection of department. name.

    so in wenserice.....there must option like this deptname:....................... invoke button

    then their respective uselist should be displayed.......

    so modify 2 webmethod query..so that this requirement can be acheived...


    2nd thing:

    other website user should be able to send PDF to these users.hope u understand what i want say?

    plz provide code for this.

    Monday, November 25, 2013 8:14 AM
  • User-1509636757 posted

    so modify 2 webmethod query..so that this requirement can be acheived...

    check the code please.. it is having it.

    and for PDF I already shared link in one of your previous post.. try exploring..

    Monday, November 25, 2013 8:39 AM