| | |
Help with data grid please
Please support our VB.NET advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
I am trying to show info at the bottom of my data grid I get error saying object reference not set to an instance of an object Here is my code Imports System.data
Imports System.data.OleDb
Imports System.Configuration
Partial Class supplier
Inherits System.Web.UI.Page
Dim ConnString As String = ConfigurationManager.AppSettings("ConnString")
Dim dbPath As String = ConfigurationManager.AppSettings("PathDB")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
getsuppliers()
dgsuppliers.DataSource = Session("Suppliers")
End If
End Sub
Private Sub getsuppliers()
Dim mySQL As String = "select * from Suppliers "
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mySQL, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Try
dbConn.Open()
dbAdapter.Fill(dsSuppliers, "Suppliers")
Catch ex As Exception
lblResults.Text = "Error occurred when retrieving data from Table: " & ex.Message
Finally
dbConn.Close()
End Try
dgsuppliers.DataSource = dsSuppliers
dgsuppliers.DataBind()
End Sub
Private Sub dgsuppliers_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgsuppliers.PageIndexChanging
End Sub
Protected Sub dgsuppliers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgsuppliers.SelectedIndexChanged
Dim mysql As String = "Select * from Suppliers where SupplierID=" & dgsuppliers.SelectedValue & ""
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mysql, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Dim supplierRow As DataRow
supplierRow = dsSuppliers.Tables("Suppliers").Rows(0)
supplierRow("CompanyName") = lblName.Text
supplierRow("ContactName") = lblContact.Text
supplierRow("ContactTitle") = lblTitle.Text
supplierRow("Address") = lblAddress.Text
supplierRow("City") = lblCity.Text
supplierRow("Region") = lblRegion.Text
supplierRow("PostalCode") = lblPostal.Text
supplierRow("Country") = lblCountry.Text
supplierRow("Phone") = lblPhone.Text
supplierRow("Fax") = lblFax.Text
supplierRow("Homepage") = lblHomepage.Text
dsSuppliers.Tables.Add("Suppliers").Rows.Add(supplierRow)
dgsuppliers.DataSource = dsSuppliers.Tables
End Sub
End Class
Imports System.data.OleDb
Imports System.Configuration
Partial Class supplier
Inherits System.Web.UI.Page
Dim ConnString As String = ConfigurationManager.AppSettings("ConnString")
Dim dbPath As String = ConfigurationManager.AppSettings("PathDB")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
getsuppliers()
dgsuppliers.DataSource = Session("Suppliers")
End If
End Sub
Private Sub getsuppliers()
Dim mySQL As String = "select * from Suppliers "
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mySQL, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Try
dbConn.Open()
dbAdapter.Fill(dsSuppliers, "Suppliers")
Catch ex As Exception
lblResults.Text = "Error occurred when retrieving data from Table: " & ex.Message
Finally
dbConn.Close()
End Try
dgsuppliers.DataSource = dsSuppliers
dgsuppliers.DataBind()
End Sub
Private Sub dgsuppliers_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgsuppliers.PageIndexChanging
End Sub
Protected Sub dgsuppliers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgsuppliers.SelectedIndexChanged
Dim mysql As String = "Select * from Suppliers where SupplierID=" & dgsuppliers.SelectedValue & ""
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mysql, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Dim supplierRow As DataRow
supplierRow = dsSuppliers.Tables("Suppliers").Rows(0)
supplierRow("CompanyName") = lblName.Text
supplierRow("ContactName") = lblContact.Text
supplierRow("ContactTitle") = lblTitle.Text
supplierRow("Address") = lblAddress.Text
supplierRow("City") = lblCity.Text
supplierRow("Region") = lblRegion.Text
supplierRow("PostalCode") = lblPostal.Text
supplierRow("Country") = lblCountry.Text
supplierRow("Phone") = lblPhone.Text
supplierRow("Fax") = lblFax.Text
supplierRow("Homepage") = lblHomepage.Text
dsSuppliers.Tables.Add("Suppliers").Rows.Add(supplierRow)
dgsuppliers.DataSource = dsSuppliers.Tables
End Sub
End Class
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 1
•
•
•
•
I am trying to show info at the bottom of my data grid I get error saying object reference not set to an instance of an object Here is my code Imports System.data
Imports System.data.OleDb
Imports System.Configuration
Partial Class supplier
Inherits System.Web.UI.Page
Dim ConnString As String = ConfigurationManager.AppSettings("ConnString")
Dim dbPath As String = ConfigurationManager.AppSettings("PathDB")
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
getsuppliers()
dgsuppliers.DataSource = Session("Suppliers")
End If
End Sub
Private Sub getsuppliers()
Dim mySQL As String = "select * from Suppliers "
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mySQL, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Try
dbConn.Open()
dbAdapter.Fill(dsSuppliers, "Suppliers")
Catch ex As Exception
lblResults.Text = "Error occurred when retrieving data from Table: " & ex.Message
Finally
dbConn.Close()
End Try
dgsuppliers.DataSource = dsSuppliers
dgsuppliers.DataBind()
End Sub
Private Sub dgsuppliers_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles dgsuppliers.PageIndexChanging
End Sub
Protected Sub dgsuppliers_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgsuppliers.SelectedIndexChanged
Dim mysql As String = "Select * from Suppliers where SupplierID=" & dgsuppliers.SelectedValue & ""
Dim dbConn As New OleDbConnection(ConnString & Server.MapPath(dbPath))
Dim dbCommand As New OleDbCommand(mysql, dbConn)
Dim dbAdapter As New OleDbDataAdapter(dbCommand)
Dim dsSuppliers As New DataSet
Dim supplierRow As DataRow
supplierRow = dsSuppliers.Tables("Suppliers").Rows(0)
supplierRow("CompanyName") = lblName.Text
supplierRow("ContactName") = lblContact.Text
supplierRow("ContactTitle") = lblTitle.Text
supplierRow("Address") = lblAddress.Text
supplierRow("City") = lblCity.Text
supplierRow("Region") = lblRegion.Text
supplierRow("PostalCode") = lblPostal.Text
supplierRow("Country") = lblCountry.Text
supplierRow("Phone") = lblPhone.Text
supplierRow("Fax") = lblFax.Text
supplierRow("Homepage") = lblHomepage.Text
dsSuppliers.Tables.Add("Suppliers").Rows.Add(supplierRow)
dgsuppliers.DataSource = dsSuppliers.Tables
End Sub
End Class
Please follow the following steps:
1. add this line in getsuppliers() function in try block
dbconn.dospose()
2. In dgsuppliers_SelectedIndexChanged function you have declared datarow. Your line of code is as follows:
Dim supplierRow As DataRow
change it as follows:
Dim supplierRow As new DataRow
or
Dim supplierRow as DataRow = New DataRow()
Your problem is solved now.
I have checked it. You have not declared the instance of datarow. Thats why it was giving an error.
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
I tried but I still have a problem. I am trying to show info at the bottom of my grid when I click on a command field in my table. I am stuck on how to finish. The design code for the grid is here
%@ Page Language="VB" AutoEventWireup="false" CodeFile="supplier.aspx.vb" Inherits="supplier" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="dgsuppliers" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames ="SupplierId"
foreColor="#333333" GridLines= "None">
<Columns>
<asp:BoundField DataField="SupplierID" HeaderText="Supplir ID " />
<asp:BoundField DataField="CompanyName" HeaderText="Supplier Name" />
<asp:CommandField SelectText="More Details" ShowSelectButton="True"/>
</Columns>
<RowStyle BackColor="#0E3AEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" Forecolor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#01CE55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor ="White" />
</asp:GridView>
<br />
<asp:Label ID="lblName" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblContact" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblAddress" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblCity" runat="server" Width="344px"></asp:Label> <br />
<asp:Label ID="lblPhone" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblFax" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblTitle" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblRegion" runat="server" Width="344px"></asp:Label> <br />
<asp:Label ID ="lblPostal" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblCountry" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblHomepage" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblResults" runat="server" Text="Label"></asp:Label>
<div>
</div>
</form>
</body>
</html>
%@ Page Language="VB" AutoEventWireup="false" CodeFile="supplier.aspx.vb" Inherits="supplier" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="dgsuppliers" runat="server" AllowPaging="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames ="SupplierId"
foreColor="#333333" GridLines= "None">
<Columns>
<asp:BoundField DataField="SupplierID" HeaderText="Supplir ID " />
<asp:BoundField DataField="CompanyName" HeaderText="Supplier Name" />
<asp:CommandField SelectText="More Details" ShowSelectButton="True"/>
</Columns>
<RowStyle BackColor="#0E3AEB" />
<EditRowStyle BackColor="#7C6F57" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" Forecolor="#333333" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#01CE55" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor ="White" />
</asp:GridView>
<br />
<asp:Label ID="lblName" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblContact" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblAddress" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblCity" runat="server" Width="344px"></asp:Label> <br />
<asp:Label ID="lblPhone" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblFax" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblTitle" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblRegion" runat="server" Width="344px"></asp:Label> <br />
<asp:Label ID ="lblPostal" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblCountry" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblHomepage" runat="server" Width="344px"></asp:Label><br />
<asp:Label ID="lblResults" runat="server" Text="Label"></asp:Label>
<div>
</div>
</form>
</body>
</html>
•
•
Join Date: Apr 2007
Posts: 3
Reputation:
Solved Threads: 1
hey i have checked it out. Okay do one thing. I am still not getting the kind of problem you have. Better mail me following things:
1. Code
2. Error message copy + paste
3. Error message print screen
4. Purpose of code.
Mail above details on taralsoni@gmail.com
Thank you.
Regards
1. Code
2. Error message copy + paste
3. Error message print screen
4. Purpose of code.
Mail above details on taralsoni@gmail.com
Thank you.
Regards
![]() |
Similar Threads
- Data grid and searching.. (Visual Basic 4 / 5 / 6)
- data grid.... very urgent...... (C#)
- How to store data in data grid to database by single mouse click in Asp.net (ASP.NET)
- Data Grid in Win32 C (C++)
- data grid problems...please help (VB.NET)
- Data Grid: How to include Hyperlinks (ASP.NET)
- refresh datagrid causes error if oper scrolled off right side of grid (VB.NET)
Other Threads in the VB.NET Forum
- Previous Thread: variables in forms
- Next Thread: Navigation (Paging) in HTML table in VB.NET.
| Thread Tools | Search this Thread |
.net .net2008 2005 2008 access account array basic beginner bing browser button buttons center check code crystalreport cuesent data database datagrid datagridview date datetimepicker designer dissertation dissertations dissertationtopic dropdownlist excel fade file-dialog filter forms ftp generatetags google hardcopy html images input insert intel internet mobile monitor ms net networking objects open output panel passingparameters pdf picturebox picturebox1 port position print printing problem save searchbox searchvb.net select serial settings shutdown soap sqlserver survey table tcp temperature text textbox timer timespan toolbox transparency trim update user vb vb.net vb.netformclosing()eventpictureboxmessagebox vb2008 vba vbnet view visual visualbasic.net visualstudio.net visualstudio2008 web winforms wpf wrapingcode year





