我正在制作一个管理部分,允许访问我项目中的几个小型SQL表。
我设置了中继器以显示我的列表,其中包含一个按钮,用于显示用于添加新条目或编辑现有条目的模态。后面的代码存储了所选行的值,然后我想查询我的SQL类以返回一个值来填充我的文本框。代码可以回溯到SQL类,我可以显示一个消息框并获得正确的结果。但是,如果查询返回到VB页面以填充文本框,我无法传递值。
这是Repeater
<%--Employee Repeater--%>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<HeaderTemplate>
<table class="display" id ="employeeList">
<thead>
<tr>
<th>
Name
</th>
<th>
Email
</th>
<th>
Update
</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("name")%>
</td>
<td>
<%# Eval("email")%>
</td>
<td>
<asp:Button ID="cmdEditName" runat="server" Text="Edit/Delete" CommandArgument= '<%#Databinder.Eval(Container.DataItem, "id")%>' OnClick="EditName"/>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
我的代码背后
'Open Name Modal bound to repeater
Public Sub EditName(ByVal sender As Object, ByVal e As System.EventArgs)
Dim wLink As New Button
Edit = True
wLink = DirectCast(sender, Button)
txtEditId.Text = wLink.CommandArgument
SQL.RunReader("SELECT name from Admin_Contacts WHERE Admin_Contacts.id = '" & txtEditId.Text & "' ")
txtEditName.Text = results
SQL.RunReader("SELECT email from Admin_Contacts WHERE Admin_Contacts.id = '" & txtEditId.Text & "' ")
txtEditEmail.Text = results
ModalName.Show()
End Sub
我的SQL类中的代码
Public Function RunReader(ByVal Query As String) As String
Dim results As String = ""
Try
SQLCon.Open()
SQLCmd = New SqlCommand(Query, SQLCon)
Dim R As SqlDataReader = SQLCmd.ExecuteReader
While R.Read
results = (R(0))
'MsgBox is just to show that I am getting results
MsgBox(results)
End While
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
答案 0 :(得分:0)
Dim results = SQL.RunReader("SELECT name from Admin_Contacts WHERE Admin_Contacts.id = '" & txtEditId.Text & "' ")
txtEditName.Text = results
答案 1 :(得分:0)
使用return语句
完成runreader函数Public Function RunReader(ByVal Query As String) As String
Dim results As String = ""
Try
SQLCon.Open()
SQLCmd = New SqlCommand(Query, SQLCon)
Dim R As SqlDataReader = SQLCmd.ExecuteReader
While R.Read
results = (R(0))
'MsgBox is just to show that I am getting results
MsgBox(results)
End While
SQLCon.Close()
Catch ex As Exception
MsgBox(ex.Message)
If SQLCon.State = ConnectionState.Open Then
SQLCon.Close()
End If
End Try
Return results
End Function
然后将此方法称为
Dim results = SQL.RunReader("SELECT name from Admin_Contacts WHERE Admin_Contacts.id = '" & txtEditId.Text & "' ")