在我的GridView中显示来自SQL Server的记录 [英] Display records from SQL Server in my GridView

查看:99
本文介绍了在我的GridView中显示来自SQL Server的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在VB.Net和ASP.Net中创建一个站点,该站点允许用户在表单上的文本框中键入代码,然后单击搜索.

当用户单击搜索时,程序将转到后端数据库(SQL Server),并在代码与在文本框中键入的代码匹配时检索所有记录.

这些信息然后全部显示在我页面上的列表框中.

例如,CentreCode | CentreName |地区代码| RegionName等
值值值值

我设法使与数据库的连接继续进行,但似乎只能显示中心代码.

到目前为止,这是我的代码:

Hello everyone,

I am creating a site in VB.Net and ASP.Net that allows a user to type a code in to a text box on my form and click search.

When the user clicks search, the program goes to the backend database(SQL Server) and retrieves all records when the code matches the code typed in to the textbox.

This information is then all displayed in to a listbox on my page.

For example, CentreCode | CentreName | RegionCode | RegionName etc
Value Value Value Value

I have managed to get the connection to the database going but can only seem to display the centrecode.

This is my code as far:

<pre lang="vb">Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim sqlConn As New SqlConnection
            Dim sqlCmd As New SqlClient.SqlCommand
            Dim sqlReader As SqlDataReader
            If TextBox1.Text = "" Then
                MsgBox("A centre code needs to be provided...")
            End If
            If TextBox1.Text <> "" Then
                ''Telling the system the location of the database.
                sqlConn.ConnectionString = "server=servername;Initial Catalog=dbname;Trusted_Connection=yes"
                ''Here we are opening the connection to the database.
                sqlConn.Open()
                MsgBox("CONNECTION OPEN..." & sqlConn.ConnectionString)
                ''This is to say that sqlCmd is a stored procedure.
                sqlCmd.CommandType = System.Data.CommandType.StoredProcedure
                ''This is creating the command to execute the stored procedure based on the information given in the connection string.
                sqlCmd = sqlConn.CreateCommand
                ''The command is triggered to execute the stored procedure which grabs all information for the specific centre.
                sqlCmd.CommandText = "exec GetAllInformation ''" & TextBox1.Text & "'' "
                MsgBox("PROCEDURE EXECUTED...")
                sqlReader = sqlCmd.ExecuteReader()
                If (sqlReader.HasRows) Then
                    While (sqlReader.Read())
                        ''This grabs the details for the specific centre from the database using the data reader.
                        GridView1.DataSource = sqlReader
                        GridView1.DataBind()
                    End While
                Else
                    MsgBox("THERE IS NO DATA TO SELECT...")
                End If
                MsgBox("INFORMATION GRABBED...")
                ''This is closing the connection to the database once we have finished with it.
                sqlConn.Close()
            End If
        End Sub




有人可以帮忙吗?

非常感谢,

Dan




Can anybody please help?

Many thanks,

Dan

推荐答案

您的sqlReader代码是错误的.您将网格绑定到返回数据中读取的每个记录上.您应该只将DataSource属性设置为ONCE.
Your sqlReader code is screwy. You''re binding the grid on every record read in the returned data. You should just set the DataSource property ONCE.
If (sqlReader.HasRows) Then
GridView1.DataSource = sqlReader
GridView1.DataBind()


如果仍无法解决问题,请尝试此操作
Try This if You Don''t able to Clear your Problem Yet
Using Reader As SqlClient.SqlDataReader = sqlCmd .ExecuteReader()
                Dim dt As New DataTable
                dt.Load(Reader)
                If dt.Rows.Count > 0 Then
                    DataGridView1.DataSource = dt
                End If
            End Using


公共无效Gridview1()
{
试试
{
DataSet ds = new DataSet();
ds = obj.GetSampleCode(//传递参数//)
如果(ds.Tables [0] .Rows.Count == 0)
{
ds.Tables [0] .Rows.Add(ds.Tables [0] .NewRow());

grd1.DataSource = ds; //设计中给定的网格ID名称
grd1.DataBind();
grd1.Rows [0] .Visible = false;

}
其他
{

grd1.DataSource = ds;
grd1.DataBind();
grd1.Visible = true;
}
}
catch(ex ex例外)
{
扔前;
}
}
public void Gridview1()
{
try
{
DataSet ds = new DataSet();
ds = obj.GetSampleCode(//Pass the Parameters//)
if (ds.Tables[0].Rows.Count == 0)
{
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

grd1.DataSource = ds; // grid ID name given in design
grd1.DataBind();
grd1.Rows[0].Visible = false;

}
else
{

grd1.DataSource = ds;
grd1.DataBind();
grd1.Visible = true;
}
}
catch (Exception ex)
{
throw ex;
}
}


这篇关于在我的GridView中显示来自SQL Server的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆