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.
Loading

knights97 reputationPosted Aug 15, 2011, 6:15 AM EDT
Jonathas Sucupira122 reputationPosted Aug 15, 2011, 6:58 AM EDT
var con = new SqlConnection(@"database=ProjectTestDB;server=HIDAYA2-PC;Integrated Security=SSPI;");
var cmd = new SqlCommand { Connection = con, CommandText = "select * from Person" };
var dt = new SqlDataAdapter(cmd);
var datable = new DataTable();
try
{
cmd.Connection.Open();
dt.Fill(datable);
}
catch (Exception)
{
throw;
}
finally
{
if (con.State == ConnectionState.Open)
return datable;
Faisal AnsariRank #3,366 · 427 reputationPosted Aug 16, 2011, 12:14 AM EDT
Jonathas Sucupira122 reputationPosted Aug 16, 2011, 5:06 AM EDT
knights97 reputationPosted Aug 16, 2011, 6:19 PM EDT