I have a stored procedure and i want to display the record to a combobox
. But it says that
There is no row at position 0 at this line.
cboSchoolYear.Text = (dt.Rows(0)("Schoolyear"))
stored procedure code:
ALTER PROCEDURE [dbo].[uspLatestDateEnrolled]
-- Add the parameters for the stored procedure here
@studID INT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT TOP 1 DateEnrolled as LatestDate,
SchoolYear,Levels,Section,StudentID
FROM StudentHistory
WHERE studentID = @studID
ORDER BY DateEnrolled DESC
END
Vb.net code
cn.Open()
Using cmd As New SqlClient.SqlCommand("uspLatestDateEnrolled", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@studID", frmView.dgv1.SelectedCells(0).Value))
cboSchoolYear.Text = (dt.Rows(0)("Schoolyear"))
cboGradeLevel.Text = (dt.Rows(1)("levels"))
cboSection.Text = (dt.Rows(2)("Section"))
dtpEnrollment.Text = (dt.Rows(3)("dateEnrolled"))
End Using
cn.Close()
While doing some research, This code solve my problem. Thank you to Codexer, for giving me some tips.
Try
cn.Open()
cmd = New SqlCommand("uspLatestDateEnrolled", cn)
cmd.Parameters.AddWithValue("@studID", frmView.dgv1.SelectedCells(0).Value)
cmd.CommandType = CommandType.StoredProcedure
da.SelectCommand = cmd
da.Fill(dt)
cboSchoolYear.Text = dt.Rows(0).Item("SchoolYear")
cboGradeLevel.Text = dt.Rows(0).Item("levels")
cboSection.Text = dt.Rows(0).Item("Section")
dtpEnrollment.Text = dt.Rows(0).Item("DateEnrolled")
Catch x As Exception
MessageBox.Show(x.GetBaseException().ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
Finally
cmd.Dispose()
cn.Close()
End Try
You've got a couple things that are wrong here (or a couple things wrong and a couple things missing from the post)
Your vb.net code should look something like this (this code isn't perfect, but its should get you moving in the correct direction)
cn.Open()
Using cmd As New SqlClient.SqlCommand("uspLatestDateEnrolled", cn)
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New SqlParameter("@studID", frmView.dgv1.SelectedCells(0).Value))
Dim SQLDataReader as SQLDataReader = cmd.executeReader()
if sqldatareader.hasrows = true then
while sqldatareader.read
if sqldatareader("Schoolyear") isnot dbnull.value then
cboSchoolYear.Text = sqldatareader("Schoolyear")
else
cboSchoolYear.Text = "Null"
end if
if sqldatareader("levels") isnot dbnull.value then
cboGradeLevel.Text = sqldatareader("levels")
else
cboGradeLevel.Text = "Null"
end if
if sqldatareader("Section") isnot dbnull.value then
cboSection.Text = sqldatareader("Section")
else
cboSection.Text = "Null"
end if
if sqldatareader("dateEnrolled") isnot dbnull.value then
cboSection.Text = sqldatareader("dateEnrolled")
else
cboSection.Text = "Null"
end if
loop
else
'No Results
end if
End Using
cn.Close()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.