为什么只显示标题,而没有查询结果?请指教!

forestless 2003-04-09 11:17:41
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.Fill(DataSet11)
DataGrid1.DataSource = DataSet11.Tables("table1")
DataGrid1.DataBind()
End If
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strvalue As String = Trim(TextBox1.Text)
If strvalue <> "" Then strvalue = "%" & strvalue & "%"
DataSet11.Tables("table1").Clear()
Dim strsql As String
Select Case DropDownList1.SelectedItem.Value
Case "课程代码"
strsql = "select * from table where 课程代码 like 'strvalue'"
SqlDataAdapter1.Fill(DataSet11, strsql)
DataGrid1.DataSource = DataSet11.Tables("table")
DataGrid1.DataBind()
Case "课程名称"
strsql = "select * from table where 课程名称 like 'strvalue'"
SqlDataAdapter1.Fill(DataSet11, strsql)
DataGrid1.DataSource = DataSet11.Tables("table")
DataGrid1.DataBind()
End Select
End Sub
...全文
77 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
forestless 2003-04-10
  • 打赏
  • 举报
回复
是参数传递不到sql语句里,那我该怎么写?
Montaque 2003-04-10
  • 打赏
  • 举报
回复
十有八九是你参数传递的不对.
比如这样写:
select bookstore.* from bookstore where 课程代码 = 123 '看一下有没有结果.
xumahua 2003-04-10
  • 打赏
  • 举报
回复
条件的字符串有问题啊,If strvalue <> "" Then strvalue = "%" & strvalue & "%"这句不要了,%号写在sql语句里。
sql = "select * from bookstore where 课程代码 like '%'+'" & strvalue & "'+'%'"
forestless 2003-04-10
  • 打赏
  • 举报
回复
to sualtring(VIP)
我把它改成
sql = "select bookstore.* from bookstore where 课程代码 = 'strvalue'
还显示不出来

to xumahua(xumahua)
能显示整个表

那为什么不能显示我要查询的记录呢?呜呼
xumahua 2003-04-10
  • 打赏
  • 举报
回复
faint!
不会连
sql = "select bookstore.* from bookstore"
都不行吧?
sualtring 2003-04-10
  • 打赏
  • 举报
回复
sql = "select * from bookstore where 课程代码 like '" & strvalue & "'"


干吗非要:strvalue = "%" & strvalue & "%" ?

如果需要将 “%”作为字符串来查询的话 还要提供关键字 “ESCAPE”
forestless 2003-04-10
  • 打赏
  • 举报
回复
好像不是dropdownlist的问题。
我在case中添了response.write语句,可以输出来的呢.
sql语句无论怎么改,datagrid中也显示不出来
xumahua 2003-04-10
  • 打赏
  • 举报
回复
SQL语句都改一下
sql = "select bookstore.* from bookstore where 课程代码 like '" & strvalue & "'"
sql = "select bookstore.* from bookstore where 课程名称 like '" & strvalue & "'"
forestless 2003-04-10
  • 打赏
  • 举报
回复
xumahua(xumahua)
不行呢。给你留言了。
forestless 2003-04-10
  • 打赏
  • 举报
回复
谢谢各位,问题总算解决了
谢谢xumahua(xumahua)
xumahua 2003-04-09
  • 打赏
  • 举报
回复
改后的代码:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.Fill(DataSet11,"table1")
DataGrid1.DataSource = DataSet11.Tables("table1")
DataGrid1.DataBind()
End If
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strvalue As String = Trim(TextBox1.Text)
If strvalue <> "" Then strvalue = "%" & strvalue & "%"
DataSet11.Tables("table1").Clear()
Dim strsql As String
Select Case DropDownList1.SelectedItem.Value
Case "课程代码"
strsql = "select * from table where 课程代码 like 'strvalue'"
SqlDataAdapter1.selectcommand.commandtext=strsql
SqlDataAdapter1.Fill(DataSet11, "table")
DataGrid1.DataSource = DataSet11.Tables("table")
DataGrid1.DataBind()
Case "课程名称"
strsql = "select * from table where 课程名称 like 'strvalue'"
SqlDataAdapter1.selectcommand.commandtext=strsql
SqlDataAdapter1.Fill(DataSet11, "table")
DataGrid1.DataSource = DataSet11.Tables("table")
DataGrid1.DataBind()
End Select
End Sub
timmy3310 2003-04-09
  • 打赏
  • 举报
回复
Select Case DropDownList1.SelectedItem.Value
改成:
Select Case DropDownList1.SelectedItem.Text.Trim()
xumahua 2003-04-09
  • 打赏
  • 举报
回复
唉~~~~你
把窗体载入的代码先屏蔽掉,只执行按钮看看能不能显示。
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strvalue As String = Trim(TextBox1.Text)
If strvalue <> "" Then strvalue = "%" & strvalue & "%"
if not DataSet11.Tables("bookstore") is nothing then
DataSet11.Tables("bookstore").Clear()
end if
Dim sql As String
Select Case DropDownList1.SelectedItem.Value
Case "课程代码"
sql = "select bookstore.* from bookstore where 课程代码 like 'strvalue'"
dim sqlAdpt as new sqlclient.sqldataadapter(sql,Me.SqlConnection1)
sqlAdpt.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
Case "课程名称"
sql = "select bookstore.* from bookstore where 课程名称 like 'strvalue'"
dim sqlAdpt as new sqlclient.sqldataadapter(sql,Me.SqlConnection1)
sqlAdpt.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
End Select
End Sub
forestless 2003-04-09
  • 打赏
  • 举报
回复
还是一样。只显示标题
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
   dataset11.tables("").clear()
sqldataadapter1.fill(dataset11)
datagrid1.datasource=dataset11.tables("bookstore")
datagrid1.databind()
end sub
把button1的click事件改成这样能显示bookstore整个表内容。
附:sqldataadapter1.fill(dataset11)改成
sqldataadapter1.fill(dataset11,"bookstore")
之后会出现错误:
异常详细信息: System.Data.ConstraintException: 未能启用约束。一行或多行中包含违反非空、唯一或外键约束的值。
xumahua 2003-04-09
  • 打赏
  • 举报
回复
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strvalue As String = Trim(TextBox1.Text)
If strvalue <> "" Then strvalue = "%" & strvalue & "%"
DataSet11.Tables("专业表").Clear()
Dim sql As String
Select Case DropDownList1.SelectedItem.Value
Case "课程代码"
sql = "select bookstore.* from bookstore where 课程代码 like 'strvalue'"
dim sqlAdpt as new sqlclient.sqldataadapter(sql,Me.SqlConnection1)
sqlAdpt.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
Case "课程名称"
sql = "select bookstore.* from bookstore where 课程名称 like 'strvalue'"
dim sqlAdpt as new sqlclient.sqldataadapter(sql,Me.SqlConnection1)
sqlAdpt.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
End Select
End Sub
这样再试试
forestless 2003-04-09
  • 打赏
  • 举报
回复
Public Class WebForm1
Inherits System.Web.UI.Page
Protected WithEvents SqlDataAdapter1 As System.Data.SqlClient.SqlDataAdapter
Protected WithEvents SqlConnection1 As System.Data.SqlClient.SqlConnection
Protected WithEvents DataSet11 As WebApplication5.DataSet1
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents Label3 As System.Web.UI.WebControls.Label
Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents SqlSelectCommand1 As System.Data.SqlClient.SqlCommand
Private currentpage As Integer
#Region " Web 窗体设计器生成的代码 "

'该调用是 Web 窗体设计器所必需的。
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.SqlDataAdapter1 = New System.Data.SqlClient.SqlDataAdapter()
Me.SqlConnection1 = New System.Data.SqlClient.SqlConnection()
Me.DataSet11 = New WebApplication5.DataSet1()
Me.SqlSelectCommand1 = New System.Data.SqlClient.SqlCommand()
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).BeginInit()
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand1
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "data source=(local);initial catalog=bookshop;persist security info=False;user id=" & _
"sa;workstation id=FOREST;packet size=4096"
'
'DataSet11
'
Me.DataSet11.DataSetName = "DataSet1"
Me.DataSet11.Locale = New System.Globalization.CultureInfo("zh-CN")
Me.DataSet11.Namespace = "http://www.tempuri.org/DataSet1.xsd"
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT 专业表.* FROM 专业表" & Microsoft.VisualBasic.ChrW(13) & Microsoft.VisualBasic.ChrW(10) & "select bookstore.* from bookstore"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
CType(Me.DataSet11, System.ComponentModel.ISupportInitialize).EndInit()

End Sub

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
'不要使用代码编辑器修改它。
InitializeComponent()
End Sub

#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
SqlDataAdapter1.Fill(DataSet11, "专业表")
DataGrid1.DataSource = DataSet11.Tables("专业表")
DataGrid1.DataBind()
End If
End Sub

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strvalue As String = Trim(TextBox1.Text)
If strvalue <> "" Then strvalue = "%" & strvalue & "%"
DataSet11.Tables("专业表").Clear()
Dim strsql As String
Select Case DropDownList1.SelectedItem.Value
Case "课程代码"
strsql = "select bookstore.* from bookstore where 课程代码 like 'strvalue'"
SqlDataAdapter1.SelectCommand.CommandText = strsql
SqlDataAdapter1.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
Case "课程名称"
strsql = "select bookstore.* from bookstore where 课程名称 like 'strvalue'"
SqlDataAdapter1.SelectCommand.CommandText = strsql
SqlDataAdapter1.Fill(DataSet11, "bookstore")
DataGrid1.DataSource = DataSet11.Tables("bookstore")
DataGrid1.DataBind()
End Select
End Sub
End Class

我的代码是这样子的。页面载入时显示“专业表”,button后,显示的只有bookstore各字段标题。请高手帮忙检查原因。加分!
forestless 2003-04-09
  • 打赏
  • 举报
回复
to xumahua(xumahua)
请问一下:在dataset11中加入了另一个表后,sqldataadapter中预览数据只能填充一个表的数据,而新加入的那个表只有标题,为什么?怎样才能在那个表中也能填充数据?


出现了两个表,可能是因为以前的两个表添加错误了,在#Region " Web 窗体设计器生成的代码 "中sqldataadapter1.selectcommand.commandtext中有两个查询语句出现,我想就因为这样才会出现两个表吧。
xumahua 2003-04-09
  • 打赏
  • 举报
回复
你确定datagrid属性都没动过?古怪的问题,那你把table1都改成table看看有什么变化?
forestless 2003-04-09
  • 打赏
  • 举报
回复
就是页面载入的时候啊
datagrid属性都没有设置的
xumahua 2003-04-09
  • 打赏
  • 举报
回复
并列的两个表?你页面载入的时候正常吗?还是按了Button1之后会出现这种情况?
加载更多回复(2)

16,556

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧