Please, can anyone help me?
I am a newbie to SQL Server Express 2005 and VWD Express, and I’m currently working on a personal project that consists of a relational database back-end (SQL SE 2005) that holds sports event data (i.e. number of participants, winner and date of event, etc), which I am trying to allow users to access certain information based on selections made in a list box control situated on a web form (VWDE).
I have a stored procedure that accepts a User Code (say ‘1’) and a Named Items string (say ‘01/04/2007 Superbowl,,10/22/2006 Kentucky Derby’) as two input parameters.
The stored procedure retrieves information about these sports and events, and, what I was hoping for, would return the relevant rows, via a sqlDataAdapter and Data Set combination, subsequently feeding a DataGrid (or GridView to use the VWDE terminology) for displaying this information via a web form.
The whole process, including some exception and error trapping, and a couple of data checking and message mechanisms, is shown below (Visual Basic).
Protected Sub ResultSetToGrid()
'Initialize a variable that holds the row count of the 'DataSet
Dim intDSRowCount As Integer = 0
'Initialize a variable that holds the column count of the 'DataSet
Dim intDSColumnCount As Integer = 0
'Initialize User Code Parameter String
Dim strUserCode As String
'Initialize selected Named Items from List Box String
Dim strSelectedEventParameter As String
'Assign '1' as value for User Code Parameter
strUserCode = "'1'"
'Set Named Items List parameter to blank
strSelectedEventParameter = ""
'Initialize counter for enumeration through selected items 'in Named Items List Box
Dim intSelectedItemCount As Integer
'Loop through each selected Named Item in List Box and add 'each one to selected Named Items list parameter
For intSelectedItemCount = 0 To Me.NamedItemsList.Items.Count - 1
If Me.NamedItemsList.Items(intSelectedItemCount).Selected Then
'If the number of selected items exceeds the index of the 'last item
'in the list box, exit the loop
If intSelectedItemCount = Me.NamedItemsList.Items.Count - 1 Then
Exit For
End If
strSelectedEventParameter = strSelectedEventParameter & Me.NamedItemsList.Items(intSelectedItemCount).Value.ToString & ","
End If
Next
'Trim the Named Items List Parameter so that it conforms
'to the appropriate format for the SP Parameter (i.e. ''Named_Item1,Named_Item2')
strSelectedEventParameter = "'" & strSelectedEventParameter.TrimEnd(",") & "'"
' Declare and initalise the Sql connection
Dim sqlConnection1 As New Data.SqlClient.SqlConnection("Data Source=HOMEOFFICE\SQLEXPRESS;Initial Catalog=SportsAlmanac;Integrated Security=True")
' Open the connection.
sqlConnection1.Open()
Try
' Declare and initalise a new SQL Command
Dim cmd As New Data.SqlClient.SqlCommand
With cmd
' Set Command text to stored procedure name
.CommandText = "sp_EvNIInList"
' Set the command type to Stored procedure
.CommandType = Data.CommandType.StoredProcedure
' Add the required SP parameters to the command.
.Parameters.Add("@UserCode", Data.SqlDbType.NChar, 10).Value = strUserCode
.Parameters("@UserCode").Direction = Data.ParameterDirection.Input
.Parameters.Add("@NIString", Data.SqlDbType.NVarChar).Value = strSelectedEventParameter
.Parameters("@NIString").Direction = Data.ParameterDirection.Input
'Set the command connection
.Connection = sqlConnection1
End With
'Declare a new SQL data adapter, passing it the stored 'procedure name, its required parameters
'and the SQL connection variable
Dim sda As New Data.SqlClient.SqlDataAdapter(cmd)
' Declare a new dataset
Dim sqlDataSet As New Data.DataSet
Try
'Fill the dataset with data from adapter
sda.Fill(sqlDataSet, "EventStats")
'Check if there are any rows in data set and if so
'Fill the form's datagrid view with data from the Dataset
'If not, throw up an error message saying zero rows returned
If sqlDataSet.Tables("EventStats").Rows.Count > 0 Then
Me.ResultSetGrid.DataSource = sqlDataSet.Tables("EventStats")
Me.ResultSetGrid.DataBind()
MsgBox("Data Found", MsgBoxStyle.OkOnly, "Data Found")
Else
MsgBox("Zero Rows Returned", MsgBoxStyle.OkOnly, "No Data Found")
End If
intDSRowCount = sqlDataSet.Tables("EventStats").Rows.Count
intDSColumnCount = sqlDataSet.Tables("EventStats").Columns.Count
Catch ex1 As Exception
' Display error message
MsgBox("Unable to retrieve Events Data (Type One). " + ex1.Message)
End Try
Catch ex2 As Exception
' Display error message
MsgBox("Unable to retrieve Events Data (Type Two). " + ex2.Message)
Throw
Finally
' Close the connection
sqlConnection1.Close()
End Try
'Check whether Named Items parameter conforms to correct 'format
Me.EventLabel.Text = strSelectedEventParameter
'Check whether User Code parameter conforms to correct ;format and assigned value is correct
Me.UserLabel.Text = strUserCode
'Count number of rows in dataset
Me.DSRowCountLabel.Text = intDSRowCount
'Count number of columns in dataset
Me.DSColumnCountLabel.Text = intDSColumnCount
End Sub
The problem is that the message box indicating that no data are in the data set (and, therefore, none are being processed through to the GridView) keeps alerting me to this fact.
The strings populating the parameters are fine, as I have checked these using the labels on the web form (i.e. the EventLabel and the UserLabel)
Also, I have added a couple of counting mechanisms (i.e. intDSRowCount and intDSColumnCount) in order to see whether there are any rows or columns existing within the “EventStats” datatable. There are no rows of data coming through, but the correct amount of columns related to the stored procedure (10) is being shown in the DSColumnCountLabel.
This would indicate to me that the dataset and stored procedure are talking to each other, but no rows of data are being returned.
I have also run the SP (called sp_EvNIInList) at source using the strings shown in the relative labels on the web form, and this runs perfectly well.
It has been suggested previously that I should use a sqlDataSource control on the web form and refer to manipulating the related GridView’s .selecting event, but the code above is a small portion of what I need in the end result, as the full caboodle will depend on a number of user selections in various other list box controls and conditional outcomes referring to numerous stored procedures and their inherent input parameters. I just need to understand why the data rows aren’t being sent through to the dataset, and how to remedy the malady.
Could some helpful soul out there please help me with this problem? I’ve been cracking up for days now. I just can’t see what I’m doing wrong.
Many thanks in advance.
DixieDean
I am a newbie to SQL Server Express 2005 and VWD Express, and I’m currently working on a personal project that consists of a relational database back-end (SQL SE 2005) that holds sports event data (i.e. number of participants, winner and date of event, etc), which I am trying to allow users to access certain information based on selections made in a list box control situated on a web form (VWDE).
I have a stored procedure that accepts a User Code (say ‘1’) and a Named Items string (say ‘01/04/2007 Superbowl,,10/22/2006 Kentucky Derby’) as two input parameters.
The stored procedure retrieves information about these sports and events, and, what I was hoping for, would return the relevant rows, via a sqlDataAdapter and Data Set combination, subsequently feeding a DataGrid (or GridView to use the VWDE terminology) for displaying this information via a web form.
The whole process, including some exception and error trapping, and a couple of data checking and message mechanisms, is shown below (Visual Basic).
Protected Sub ResultSetToGrid()
'Initialize a variable that holds the row count of the 'DataSet
Dim intDSRowCount As Integer = 0
'Initialize a variable that holds the column count of the 'DataSet
Dim intDSColumnCount As Integer = 0
'Initialize User Code Parameter String
Dim strUserCode As String
'Initialize selected Named Items from List Box String
Dim strSelectedEventParameter As String
'Assign '1' as value for User Code Parameter
strUserCode = "'1'"
'Set Named Items List parameter to blank
strSelectedEventParameter = ""
'Initialize counter for enumeration through selected items 'in Named Items List Box
Dim intSelectedItemCount As Integer
'Loop through each selected Named Item in List Box and add 'each one to selected Named Items list parameter
For intSelectedItemCount = 0 To Me.NamedItemsList.Items.Count - 1
If Me.NamedItemsList.Items(intSelectedItemCount).Selected Then
'If the number of selected items exceeds the index of the 'last item
'in the list box, exit the loop
If intSelectedItemCount = Me.NamedItemsList.Items.Count - 1 Then
Exit For
End If
strSelectedEventParameter = strSelectedEventParameter & Me.NamedItemsList.Items(intSelectedItemCount).Value.ToString & ","
End If
Next
'Trim the Named Items List Parameter so that it conforms
'to the appropriate format for the SP Parameter (i.e. ''Named_Item1,Named_Item2')
strSelectedEventParameter = "'" & strSelectedEventParameter.TrimEnd(",") & "'"
' Declare and initalise the Sql connection
Dim sqlConnection1 As New Data.SqlClient.SqlConnection("Data Source=HOMEOFFICE\SQLEXPRESS;Initial Catalog=SportsAlmanac;Integrated Security=True")
' Open the connection.
sqlConnection1.Open()
Try
' Declare and initalise a new SQL Command
Dim cmd As New Data.SqlClient.SqlCommand
With cmd
' Set Command text to stored procedure name
.CommandText = "sp_EvNIInList"
' Set the command type to Stored procedure
.CommandType = Data.CommandType.StoredProcedure
' Add the required SP parameters to the command.
.Parameters.Add("@UserCode", Data.SqlDbType.NChar, 10).Value = strUserCode
.Parameters("@UserCode").Direction = Data.ParameterDirection.Input
.Parameters.Add("@NIString", Data.SqlDbType.NVarChar).Value = strSelectedEventParameter
.Parameters("@NIString").Direction = Data.ParameterDirection.Input
'Set the command connection
.Connection = sqlConnection1
End With
'Declare a new SQL data adapter, passing it the stored 'procedure name, its required parameters
'and the SQL connection variable
Dim sda As New Data.SqlClient.SqlDataAdapter(cmd)
' Declare a new dataset
Dim sqlDataSet As New Data.DataSet
Try
'Fill the dataset with data from adapter
sda.Fill(sqlDataSet, "EventStats")
'Check if there are any rows in data set and if so
'Fill the form's datagrid view with data from the Dataset
'If not, throw up an error message saying zero rows returned
If sqlDataSet.Tables("EventStats").Rows.Count > 0 Then
Me.ResultSetGrid.DataSource = sqlDataSet.Tables("EventStats")
Me.ResultSetGrid.DataBind()
MsgBox("Data Found", MsgBoxStyle.OkOnly, "Data Found")
Else
MsgBox("Zero Rows Returned", MsgBoxStyle.OkOnly, "No Data Found")
End If
intDSRowCount = sqlDataSet.Tables("EventStats").Rows.Count
intDSColumnCount = sqlDataSet.Tables("EventStats").Columns.Count
Catch ex1 As Exception
' Display error message
MsgBox("Unable to retrieve Events Data (Type One). " + ex1.Message)
End Try
Catch ex2 As Exception
' Display error message
MsgBox("Unable to retrieve Events Data (Type Two). " + ex2.Message)
Throw
Finally
' Close the connection
sqlConnection1.Close()
End Try
'Check whether Named Items parameter conforms to correct 'format
Me.EventLabel.Text = strSelectedEventParameter
'Check whether User Code parameter conforms to correct ;format and assigned value is correct
Me.UserLabel.Text = strUserCode
'Count number of rows in dataset
Me.DSRowCountLabel.Text = intDSRowCount
'Count number of columns in dataset
Me.DSColumnCountLabel.Text = intDSColumnCount
End Sub
The problem is that the message box indicating that no data are in the data set (and, therefore, none are being processed through to the GridView) keeps alerting me to this fact.
The strings populating the parameters are fine, as I have checked these using the labels on the web form (i.e. the EventLabel and the UserLabel)
Also, I have added a couple of counting mechanisms (i.e. intDSRowCount and intDSColumnCount) in order to see whether there are any rows or columns existing within the “EventStats” datatable. There are no rows of data coming through, but the correct amount of columns related to the stored procedure (10) is being shown in the DSColumnCountLabel.
This would indicate to me that the dataset and stored procedure are talking to each other, but no rows of data are being returned.
I have also run the SP (called sp_EvNIInList) at source using the strings shown in the relative labels on the web form, and this runs perfectly well.
It has been suggested previously that I should use a sqlDataSource control on the web form and refer to manipulating the related GridView’s .selecting event, but the code above is a small portion of what I need in the end result, as the full caboodle will depend on a number of user selections in various other list box controls and conditional outcomes referring to numerous stored procedures and their inherent input parameters. I just need to understand why the data rows aren’t being sent through to the dataset, and how to remedy the malady.
Could some helpful soul out there please help me with this problem? I’ve been cracking up for days now. I just can’t see what I’m doing wrong.
Many thanks in advance.
DixieDean