Faisal Ansari

Faisal Ansari

  • NA
  • 451
  • 831.7k

How can we create Web Service with Database

Aug 15 2011 6:05 AM
hello guys how r u ?

my question is that,

i am creating a web service which is fetching data from database and retrive to client,

my code is below

Web service file
---------------

    [WebMethod]
    public DataTable DataService()
    {
        SqlConnection con = new SqlConnection(@"Data Source=HIDAYA2-PC; Integrated Security=True; Database=ProjectTestDB;");
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;
        cmd.Connection.Open();
        cmd.CommandText = "select * from Person";
        SqlDataReader reader = cmd.ExecuteReader();
        DataTable dt = new DataTable();
        if (reader.HasRows)
        {
            dt.Load(reader);
        }
        cmd.Connection.Close();
        return dt;
    }

Default.aspx.cs
---------------

    protected void Page_Load(object sender, EventArgs e)
    {
        WebService1.Service service = new WebService1.Service();
        GridView1.DataSource = service.DataService();
        GridView1.DataBind();
       
    }

my problem is that an exception accured over there

reply soon

thanks.


Answers (5)