我的应用程序包含一个GridView,其中包含下拉列表控件和文本框控件。尝试的是:当用户从下拉列表中选择一个项目时,文本框控件将填充项目描述。我可以通过以下代码完成任务:
Protected Sub ddlStartPartDescrip_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlStartPartDescrip As String = DirectCast(gvPartDetailDescription.Controls(0).Controls(0).FindControl("ddlStartPartDescrip"), DropDownList).Text
Dim myConnectionStringIII As String = ConfigurationManager.ConnectionStrings("ShipperNotificationConnectionString").ConnectionString.ToString
Dim myConnectionIII As New SqlConnection(myConnectionStringIII)
myConnectionIII.Open()
Dim mySQLQueryIII As String = "SELECT Part_Name, Part_Desc FROM Part WHERE Part_Name='" + ddlStartPartDescrip.ToString() + "'" + "ORDER BY Part_Name"
Dim myCommandIII As New SqlCommand(mySQLQueryIII, myConnectionIII)
Dim myReaderIII As SqlDataReader = myCommandIII.ExecuteReader()
While (myReaderIII.Read())
'the orginal code
Dim row As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)
Dim txtStartPartDescripNum As TextBox = DirectCast(row.FindControl("txtStartPartDescripNum"), TextBox)
txtStartPartDescripNum.Text = myReaderIII.GetValue(1).ToString
End While
myReaderIII.Close()
myConnectionIII.Close()
End Sub
...但是,我正在尝试通过存储过程完成相同的任务。我的问题是,当从下拉列表中选择列表项时,文本框控件不会呈现任何描述。
这是我的存储过程:
USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Part_Name_Descript]
(
@ddlStartPartDescrip varchar(250)
)
AS
Begin
SELECT Part_Name, Part_Desc FROM Part WHERE Part_Name='" + @ddlStartPartDescrip + "'
END
这是后面的代码,它与非存储过程方法一样放在ddlStartPartDescrip_SelectedIndexChanged中:
Dim con As New SqlConnection(GetConnectionString())
'open connection
con.Open()
Dim cmd As New SqlCommand("Part_Name_Descript", con)
cmd.CommandType = CommandType.StoredProcedure
Dim p1 As New SqlParameter("@ddlStartPartDescrip", ddlStartPartDescrip)
cmd.Parameters.Add(p1)
Dim rd As SqlDataReader = cmd.ExecuteReader()
While (rd.Read())
Dim row As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)
Dim txtStartPartDescripNum As TextBox = DirectCast(row.FindControl("txtStartPartDescripNum"), TextBox)
txtStartPartDescripNum.Text = rd.GetValue(1).ToString
End While
con.Close()
...请问我能错过什么/做错了吗?提前致谢
答案 0 :(得分:1)
您的查询
SELECT Part_Name, Part_Desc FROM Part WHERE Part_Name='" + @ddlStartPartDescrip + "'
正在寻找Part_Name
这是文字字符串"@ddlStartPartDescrip"
。你需要使用
SELECT Part_Name, Part_Desc FROM Part WHERE Part_Name = @ddlStartPartDescrip
编辑:顺便说一句,您已经拥有Part_Name
,因此选择它可能是多余的。