I have a stored procedure which is generating a attendance report (in sql server 2012).
now I have to execute this stored procedure from my VB.NET application and display the results in a DataGrid View. help me out to display the result in the datagrid view in vb.
This is the code for stored procedure:
<pre>USE [first_db]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[GET_ATTENDANCE]
@STARTDATE DATETIME,
@ENDDATE DATETIME
AS BEGIN
;WITH DATERANGE AS
(
SELECT DT =DATEADD(DD,0, @STARTDATE)
WHERE DATEADD(DD, 1, @STARTDATE) <= @ENDDATE
UNION ALL
SELECT DATEADD(DD, 1, DT)
FROM DATERANGE
WHERE DATEADD(DD, 1, DT) <= @ENDDATE
)
SELECT * INTO #TMP_DATES
FROM DATERANGE
DECLARE @COLUMN VARCHAR(MAX)
SELECT @COLUMN=ISNULL(@COLUMN+',','')+ '['+ CAST(CONVERT(DATE , T.DT) AS VARCHAR) + ']' FROM #TMP_DATES T
DECLARE @Columns2 VARCHAR(MAX)
SET @Columns2 = SUBSTRING((SELECT DISTINCT ',ISNULL(['+ CAST(CONVERT(DATE , DT) as varchar )+'],''A'') AS ['+CAST(CONVERT(DATE , DT) as varchar )+']' FROM #TMP_DATES GROUP BY dt FOR XML PATH('')),2,8000)
DECLARE @QUERY VARCHAR(MAX)
SET @QUERY = 'SELECT User_Id, ' + @Columns2 +' FROM
(
SELECT A.User_Id , B.DT AS DATE, A.STATUS FROM MarkA A RIGHT OUTER JOIN #TMP_DATES B ON A.DATE=B.DT
) X
PIVOT
(
MIN([STATUS])
FOR [DATE] IN (' + @COLUMN + ')
) P
WHERE ISNULL(User_Id,'''')<>''''
'
EXEC (@QUERY)
DROP TABLE #TMP_DATES
END
GO
What I have tried:
this is the code in vb:
Private Sub StartDatePicker_ValueChanged(sender As Object, e As EventArgs) Handles StartDatePicker.ValueChanged
' StartDate = Format(StartDatePicker.Value, "yyyy/mm/dd")
sd = Date.Parse(StartDatePicker.Text)
End Sub
Private Sub EndDatePicker_ValueChanged(sender As Object, e As EventArgs) Handles EndDatePicker.ValueChanged
'EndDate = Format(EndDatePicker.Value, "yyyy/mm/dd")
ed = Date.Parse(EndDatePicker.Text)
End Sub
Private Sub Show_Button_Click(sender As Object, e As EventArgs) Handles Show_Button.Click
Try
Dim dt As DataTable = New DataTable()
Dim cmd As SqlCommand = New SqlCommand("GET_ATTENDANCE", connection)
cmd.CommandType = CommandType.StoredProcedure
cmd.Connection = connection
cmd.Parameters.AddWithValue("@STARTDATE", sd)
cmd.Parameters.AddWithValue("@ENDDATE", ed)
connection.Open()
Dim adp As New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
cmd.ExecuteNonQuery()
adp.Fill(ds, "GET_ATTENDANCE")
DataGridView1.DataSource = ds.Tables(0)
connection.Close()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class
I have a form which selects the start date and end date from the user and displays the result in the data grid view.But this code neither generates an error nor an exception is thrown ?? so what should i do (results are not getting displayed in the datagridview)