Page 1 of 1

Dataset Textbox Integration

Posted: Sun 20 Mar 2005 04:56
by Nikko
:cry:

I am very new to all this and hope someone can help me. I am sure I am doing something stupid but I have spent many hours reading to figure out what I am doing wrong and realize I need help.

All I want to do is load data into a bunch of text fields on the screen based on loginID and let users update them back to the database.

I created a simple ASPX page to demonstate my issue and copied it below:

Code: Select all


Imports CoreLab.MySql
Imports System.Data


Public Class test
    Inherits System.Web.UI.Page


#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
     Private Sub InitializeComponent()

    End Sub
    Protected WithEvents myGrid As System.Web.UI.WebControls.DataGrid
    Protected WithEvents txTest1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents txTest2 As System.Web.UI.WebControls.TextBox

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

        Dim MyDS As New DataSet
        Dim connStr As String
        Dim SQL As String
        connStr = "User Id=test;Password=test;Host=localhost;Database=test;"
        SQL = "SELECT CompanyName FROM company where companyID = 1"
        Dim conn As MySqlConnection = New MySqlConnection(connStr)
        Dim Adapter As New MySqlDataAdapter(SQL, connStr)
        Adapter.Fill(MyDS)
        myGrid.DataSource = MyDS
        myGrid.DataBind()

        Dim myTable As DataTable
        Dim myRow As DataRow
        Dim myCol As DataColumn
        Me.txTest1.Text = myRow.Item(1)         '---------------- errors out 
        Me.txTest2.Text = myRow("CompanyName")  '---------------- errors out
    End Sub

End Class
[/color]

When I run the code I get the error below. I know the dataset is loading properly as it shows up in the Datagrid Properly if I remove the offending statements.

Code: Select all

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 47:         Dim myRow As DataRow
Line 48:         Dim myCol As DataColumn
Line 49:         Me.txTest1.Text = myRow.Item(1)         '---------------- errors out 
Line 50:         Me.txTest2.Text = myRow("CompanyName")  '---------------- errors out
Line 51:     End Sub
 

Source File: c:\inetpub\wwwroot\test\test.aspx.vb    Line: 49 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   eFraud.test.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\test\test.aspx.vb:49
   System.Web.UI.Control.OnLoad(EventArgs e)
   System.Web.UI.Control.LoadRecursive()
   System.Web.UI.Page.ProcessRequestMain()
[/color]


Many thanks for your help!!

Posted: Tue 22 Mar 2005 12:59
by Serious
Please pay attention that myRow and myCol variables in your sample are NULL.
Please try to use this simple code fragment. It is based on "test" schema included in MySQLDirect .NET distribution package (see Program Files\CoreLab\MySQLDirect.NET\Samples\tables.sql)

Code: Select all

dim connection as MySqlConnection = new MySqlConnection("host=localhost;database=test;user id=root;")
dim cmd as MySqlCommand = new MySqlCommand("select * from dept", connection)
rem dim reader as MySqlDataReader
dim dataAdapter as MySqlDataAdapter = new MySqlDataAdapter(cmd)
dim dataSet as DataSet = new DataSet()
dataAdapter.Fill(dataSet, "dept")
dim dataTable as DataTable = dataSet.Tables("dept")
dim dataRow as DataRow = dataTable.Rows(0)

Console.WriteLine(dataRow(0))
Console.WriteLine(dataRow.Item(1))
Console.WriteLine(dataRow.Item("loc"))