My datagrid didn't show data,why?please see my code.
I use asp.net and sql server 2000
Problems are:
I debug my web page but datagrid control didn't show any data from my database. and if i would like to create add, delete update and search button for datagrid, please give me a advices or show me...
Thanks
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Mypage.aspx.vb" Inherits="Myproject.Mypage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Mypage</title>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
'declarations
Dim conLath As SqlConnection
Dim cmdLath As SqlCommand
Dim rdrLath As SqlDataReader
conLath = New SqlConnection( _
"server=local;uid=;pwd=; " & _
"database=MySQLDB")
conLath.Open
cmdLath = New SqlCommand ( _
"select field1,field2 from MyTable", _
conLath)
rdrLath = cmdLath.ExecuteReader
gridLath.DataSource = rdrLath
gridLath.DataBind
rdrLath.Close
cmdLath.Dispose
conLath.Close
End Sub
</script>
<html>
<head>
<title>Test</title>
</head>
<asp:DataGrid id="gridLath" runat="server"></asp:DataGrid>
</body>
</html>
Re: My datagrid didn't show data,why?please see my code.
Quote:
Originally Posted by wattha
Code:
<HTML>
<HEAD>
<title>Mypage</title>
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
'declarations
Dim conLath As SqlConnection
Dim cmdLath As SqlCommand
Dim rdrLath As SqlDataReader
conLath = New SqlConnection( _
"server=local;uid=;pwd=; " & _
"database=MySQLDB")
conLath.Open
cmdLath = New SqlCommand ( _
"select field1,field2 from MyTable", _
conLath)
rdrLath = cmdLath.ExecuteReader
gridLath.DataSource = rdrLath
gridLath.DataBind
rdrLath.Close
cmdLath.Dispose
conLath.Close
End Sub
</script>
<html>
<head>
<title>Test</title>
</head>
<body>
<asp:DataGrid id="gridLath" runat="server"></asp:DataGrid>
</body>
</html>
a) You are missing a <body> tag after the </head> tag.
b) You are writing "Server=local" in the connection string. Replace local with the actual server name or if the SQL server is on the same machine then you could use "Server=(local)"
Just to clarify are you getting a blank page or an error message ?
Re: My datagrid didn't show data,why?please see my code.
hi, Nikhil Aggarwal
Thanks but my problem is My page can debug not error appear, The other controls appear on page but just data grid didn't and not shown any data from database.
And i defined exaclty the server name, like wattha, if i would like to copy this project and Database files to open on another computer , It's may be error?? because of conflict server name????
Re: My datagrid didn't show data,why?please see my code.
Hi,
I use datagrid when debug the page, Every controls are display normally except datagrid. It's not show any data from database, No error message appear, why & what wrong on my code?
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Myweb.aspx.vb" Inherits="Myproj.Myweb" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Data" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>Myweb</title>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim DS As DataSet
Dim MyConnection As SqlConnection
Dim MyCommand As SqlDataAdapter
MyConnection = New SqlConnection("server=(local)\NetSDK;database=MyDB;Trusted_Connection=yes")
MyCommand = New SqlDataAdapter("select * from MyTable", MyConnection)
DS = new DataSet()
MyCommand.Fill(ds, "MyTable")
MyDataGrid.DataSource=ds.Tables("MyTable").DefaultView
MyDataGrid.DataBind()
End Sub
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="MyDatagrid" runat="server" Width="700" BackColor="#ccccff" BorderColor="black"
ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd"
EnableViewState="false" style="Z-INDEX: 112; LEFT: 248px; POSITION: absolute; TOP: 184px" />
</form>
</body>
</HTML>
Re: My datagrid didn't show data,why?please see my code.
can you use a sqlcommand against mySQL. shouldn't you be using oledb?
Re: My datagrid didn't show data,why?please see my code.
Re: My datagrid didn't show data,why?please see my code.
1. I would suggest you use the Code Behind files to actully write the VB code instead of the aspx. page.
2. Check if the dataset actually returns any rows. If it returns ZERO, then the datagrid wont be displayed.
Re: My datagrid didn't show data,why?please see my code.
I agree, it is so much easier, and cleaner looking by writing in codebehind rather than the aspx...
Re: My datagrid didn't show data,why?please see my code.
This maybe a stupid question, but I'll ask anyways, since it's the only thing that stands out to me: Can you bind a DataREADER to objects? Not only that, but it's defined within the Form_Load event, so that when it's done, it's going to fall out of scope and no longer be available.....
Tg
Re: My datagrid didn't show data,why?please see my code.
Never mind, I see now that it's been changed to a Dataset.... but I'd still have concerns that the DS variable is falling out of scope.
Tg
Re: My datagrid didn't show data,why?please see my code.
Quote:
but I'd still have concerns that the DS variable is falling out of scope.
I dont see where?