Store Proc Problem
-
I want to use a Gridview for a standard CRUD app. So far I have my delete event wired up but I have a problem with my stored proc. I receive this error everytime i run my debugger and click the delete event. I have a catch event to catch this error. The SqlParameterCollection only accepts non-null SqlParameter type objects, not Parameter objects. Here is the code for my deleterow event. Any help is appreciated. protected void DeleteRow(object sender, GridViewDeleteEventArgs e) { try { SqlConnection connection = new SqlConnection(); connection.ConnectionString = ConfigurationManager.ConnectionStrings["PokerTraxConnectionString"].ToString(); connection.Open(); SqlCommand delCommand = new SqlCommand(); delCommand.Connection = connection; delCommand.CommandText = "usp_Delete_Loan"; delCommand.CommandType = CommandType.StoredProcedure; delCommand.Parameters.Add(new Parameter("LoanId")); delCommand.ExecuteNonQuery(); SqlDataAdapter adapter = new SqlDataAdapter(delCommand); DataSet ds = new DataSet(); GridView1.DataSource = ds; adapter.Fill(ds); Cache["Loan"] = ds; BindPlayerNames(); connection.Close(); connection.Dispose(); delCommand.Dispose(); } catch (Exception ex) { errorMessage.Text = ex.Message; } }
-
I want to use a Gridview for a standard CRUD app. So far I have my delete event wired up but I have a problem with my stored proc. I receive this error everytime i run my debugger and click the delete event. I have a catch event to catch this error. The SqlParameterCollection only accepts non-null SqlParameter type objects, not Parameter objects. Here is the code for my deleterow event. Any help is appreciated. protected void DeleteRow(object sender, GridViewDeleteEventArgs e) { try { SqlConnection connection = new SqlConnection(); connection.ConnectionString = ConfigurationManager.ConnectionStrings["PokerTraxConnectionString"].ToString(); connection.Open(); SqlCommand delCommand = new SqlCommand(); delCommand.Connection = connection; delCommand.CommandText = "usp_Delete_Loan"; delCommand.CommandType = CommandType.StoredProcedure; delCommand.Parameters.Add(new Parameter("LoanId")); delCommand.ExecuteNonQuery(); SqlDataAdapter adapter = new SqlDataAdapter(delCommand); DataSet ds = new DataSet(); GridView1.DataSource = ds; adapter.Fill(ds); Cache["Loan"] = ds; BindPlayerNames(); connection.Close(); connection.Dispose(); delCommand.Dispose(); } catch (Exception ex) { errorMessage.Text = ex.Message; } }
This could be that the parameter was not created completey. In code that I have used I set other values for parameter object. Generic stored proc calling code:
Dim command As New System.Data.SqlClient.SqlCommand command.Connection = m\_sqlConnection command.CommandText = strStoredProc command.CommandType = CommandType.StoredProcedure ' declare the parameter object Dim param As System.Data.SqlClient.SqlParameter Dim myEnumerator As System.Collections.IEnumerator = \_ parameterArray.GetEnumerator() Dim pItem As ParameterItem While myEnumerator.MoveNext() pItem = CType(myEnumerator.Current, ParameterItem) ' Add a new parameter, get back a reference to it param = command.Parameters.Add(pItem.Name, pItem.Type) ' set the parameter's direction and value param.Direction = pItem.Direction param.Value = pItem.Value End While Dim myDataAdapter As New SqlClient.SqlDataAdapter command.CommandTimeout = iTimeOut myDataAdapter.SelectCommand = command myDataAdapter.Fill(ds, "ResultSet")
-
This could be that the parameter was not created completey. In code that I have used I set other values for parameter object. Generic stored proc calling code:
Dim command As New System.Data.SqlClient.SqlCommand command.Connection = m\_sqlConnection command.CommandText = strStoredProc command.CommandType = CommandType.StoredProcedure ' declare the parameter object Dim param As System.Data.SqlClient.SqlParameter Dim myEnumerator As System.Collections.IEnumerator = \_ parameterArray.GetEnumerator() Dim pItem As ParameterItem While myEnumerator.MoveNext() pItem = CType(myEnumerator.Current, ParameterItem) ' Add a new parameter, get back a reference to it param = command.Parameters.Add(pItem.Name, pItem.Type) ' set the parameter's direction and value param.Direction = pItem.Direction param.Value = pItem.Value End While Dim myDataAdapter As New SqlClient.SqlDataAdapter command.CommandTimeout = iTimeOut myDataAdapter.SelectCommand = command myDataAdapter.Fill(ds, "ResultSet")
Perhaps I should have mentioned I wrote this in C#.net. Thanks for the help though I can kindve determine how this is supposed to work.