[英]Retrieving record to a combo box in vb.net
I have a stored procedure and i want to display the record to a combobox
. 我有一个存储过程,我想将记录显示到combobox
。 But it says that 但是它说
There is no row at position 0 at this line. 在此行的位置0没有行。
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 Vb.net代码
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. 感谢Codexer给我一些提示。
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) 您的vb.net代码应如下所示(此代码虽然不完美,但可以使您朝正确的方向前进)
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.