I am using mvc-5 with angularjs i have a class DBFunction.cs in which i have a method like
public SqlDataReader ExecuteSP_Reader(string ProcedureName, SqlCommand MyCommand)
{
if (con.State == ConnectionState.Closed)
con.Open();
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.CommandText = ProcedureName;
//MyCommand.Connection = (SqlConnection)HttpContext.Current.Application["con"];
MyCommand.Connection = con;
SqlDataReader _Reader = MyCommand.ExecuteReader();
//con.Close();
return _Reader;
}
and another class called task
i am fetching the data from the database from the method of the class
public List<object>getdatabyuser(string UserAutoId=null)
{
cmd = new SqlCommand();
List<object> getdatauser = new List<object>();
if(!string.IsNullOrEmpty(UserAutoId))
cmd.Parameters.AddWithValue("@userautoid", UserAutoId);
SqlDataReader dr;
dr = dbf.ExecuteSP_Reader("TMS_GETTASKBYUSERNAME", cmd);
while(dr.Read())
{
getdatauser.Add(new
{
TaskName=dr["TaskName"].ToString(),
ParentAutoID=dr["ParentAutoID"].ToString(),
UserAutoId = dr["UserAutoId"].ToString(),
AssignBy=dr["AssignBy"].ToString(),
AssignTo=dr["AssignTo"].ToString()
});
}
dr.Close();
return getdatauser;
}
and there is another layer in this, i used a webservice called dashboard.asmx
[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public void gettaskbyuser(string UserAutoId)
{
var json = "";
json = js.Serialize(objtask.getdatabyuser(UserAutoId));
Context.Response.Write(json);
}
here i am converting the data into json format an sending the data to my controller of angularjs
and here is my controller
$http.get('/WebServices/dashboard.asmx/gettaskbyuser', {
params: {
UserAutoId: $scope.showparam.UserAutoId
}
}).then(function (response) {
$scope.getdatainmodal = response.data;
})
the data will be shown on button click,
i have a table
Azhar 4
Das 1
Minesh 1
<td><a href="#" ng-click="showmodal(x)" data-toggle="modal" data-target="#myModal">{{ x.TaskCount }}</a></td>
there is an a tag in my table and when user click that, modal pops up with the data
<table class="table table-bordered table-responsive">
<tr class="bg-primary">
<td><center><span class="glyphicon glyphicon-edit"></span></center></td>
<td>TaskName</td>
<td>User Name</td>
<td>AssignBy</td>
<td>AssignTo</td>
<td ng-hide="true">Parent Id</td>
</tr>
<tr ng-repeat="x in getdatainmodal">
<td><center><a href="#" class="btn btn-success">Edit</a></center></td>
<td>{{x.TaskName}}</td>
<td>{{x.UserAutoId}}</td>
<td>{{x.AssignBy}}</td>
<td>{{x.AssignTo}}</td>
<td ng-hide="true">{{x.ParentAutoID}}</td>
</tr>
</table>
which works fine for the first time, but when a user clicks it again for the second time, for other user's record, it is showing an exceptions
Possibly unhandled rejection: {"data":"System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
what is wrong in this code?