Webservice, already an open DataReader
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace StudentDAL
{
public class Student
{
string connString;
SqlConnection connection = null;
public Student()
{
connString =
ConfigurationSettings.AppSettings["ConnectionString"].ToString();
}
private SqlConnection connectToDB()
{
SqlConnection conn = new SqlConnection(connString);
conn.Open();
return conn;
}
public void InsertStudent(int studNo, string studName)
{
SqlCommand cmd = new SqlCommand();
if (connection == null)
{
connection = connectToDB();
}
cmd.Connection = connection;
cmd.CommandText = "Insert into Student values(" + studNo +
",'" + studName + "')";
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
public int UpdateStudent(int studNo, string studName)
{
SqlCommand cmd = new SqlCommand();
if (connection == null)
{
connection = connectToDB();
}
cmd.Connection = connection;
cmd.CommandText = "Update Student set StudentName = '" +
studName + "' where StudentNo=" + studNo;
int noRowsAffected = cmd.ExecuteNonQuery();
return noRowsAffected;
}
public int DeleteStudent(int studNo)
{
SqlCommand cmd = new SqlCommand();
if (connection == null)
{
connection = connectToDB();
}
cmd.Connection = connection;
cmd.CommandText = "Delete Student where StudentNo=" + studNo;
cmd.CommandType = CommandType.Text;
int noRowsAffected = cmd.ExecuteNonQuery();
return noRowsAffected;
}
public SqlDataReader ListOfStudents()
{
SqlCommand cmd = new SqlCommand();
if (connection == null)
{
connection = connectToDB();
}
cmd.Connection = connection;
cmd.CommandText = "Select * from Student";
cmd.CommandType = CommandType.Text;
SqlDataReader dr =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
public bool ValidateUser(string uName)
{
SqlCommand cmd = new SqlCommand();
if (connection == null)
{
connection = connectToDB();
}
cmd.Connection = connection;
cmd.CommandText = "Select * from Users where UserName='" +
uName + "'";
cmd.CommandType = CommandType.Text;
SqlDataReader dr =
cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (dr.HasRows)
{
return true;
}
else
{
return false;
}
}
}
}
System.Web.Services.Protocols.SoapException: Server was unable to process
request. ---> System.InvalidOperationException: There is already an open
DataReader associated with this Command which must be closed first. at
System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand
command) at
System.Data.SqlClient.SqlConnection.ValidateConnectionForExecute(String
method, SqlCommand command) at
System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean
async) at
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult
result, String methodName, Boolean sendToPipe) at
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() at
StudentDAL.Student.InsertStudent(Int32 studNo, String studName) in
C:\Users\Naidu's\Documents\Visual Studio
2010\Projects\MohanNew\StudentDAL\StudentDAL\Class1.cs:line 34 at
MyStudentWebService.Service1.InsertStudent(Int32 studNo, String sName,
String uName) in c:\users\naidu's\documents\visual studio
2010\Projects\MohanNew\MyStudentWebService\MyStudentWebService\Service1.asmx.cs:line
29 --- End of inner exception stack trace ---
I am getting this in my web page i could not find anything in my code.
No comments:
Post a Comment