Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Pulling specific columns from a dataset. using vb.net and MySQL

  • 10-07-2005 1:26pm
    #1
    Closed Accounts Posts: 18


    Hello,

    The below code pullsfills the Content text box with the value of the PageText column using a select statement which only returns that column however if I want to extract everything from that row how do I then extract the value of the PageText column. I read somewhere that this can be done by referencing the column number. Is that right? I'm open to any suggestions. I do want to use a dataset as its not forward only. I've looked at using typed datasets but at the moment I cant find an example to learn from that's using the mysql connecter.

    Any help welcome. to show how far I've got I've attached some code below.
    Imports MySql.Data.MysqlClient
    Imports System.Data
    Imports System




    Public Class WebForm1
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Content As System.Web.UI.WebControls.Label
    '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
    Dim DataSet As New DataSet
    Dim TitleRow As DataRow
    Dim TitleTable As DataTable
    Dim conn As MySqlConnection
    Dim DataAdapter As MySqlDataAdapter
    Dim SQLCMD As String

    SQLCMD = "SELECT PageText FROM Content Where ID = 1;"

    Try
    conn = New MySqlConnection
    conn.ConnectionString = "server=localhost;" _
    & "user id=User;" _
    & "password=User;" _
    & "database=DataBase"
    DataAdapter = New MySqlDataAdapter(SQLCMD, conn)
    conn.Open()
    DataSet = New DataSet
    DataAdapter.Fill(DataSet, "Content")
    Dim CurrentTable As DataTable
    Dim CurrentRow As DataRow
    Dim CurrentColumn As DataColumn
    For Each CurrentTable In DataSet.Tables
    For Each CurrentRow In CurrentTable.Rows
    For Each CurrentColumn In CurrentTable.Columns
    If Not CurrentRow(CurrentColumn) Is Nothing Then
    If Not IsDBNull(CurrentRow(CurrentColumn)) Then
    Content.Text = Convert.ToString(CurrentRow(CurrentColumn))
    Else
    Content.Text = "Null"
    End If
    End If
    Next
    Next
    Next
    Catch myerror As MySqlException

    Finally
    conn.Close()
    conn.Dispose()
    End Try
    End Sub
    End Class


Comments

  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    i can do it in C#, but they way i'm currently doing it in my project involves reading the data into a dataset and then referencing that dataset with a new dataview. So i end up taking whatever info i need from the dataview (who's source is the dataset).


  • Registered Users, Registered Users 2 Posts: 640 ✭✭✭Kernel32


    Hello,

    The below code pullsfills the Content text box with the value of the PageText column using a select statement which only returns that column however if I want to extract everything from that row how do I then extract the value of the PageText column. I read somewhere that this can be done by referencing the column number. Is that right? I'm open to any suggestions. I do want to use a dataset as its not forward only. I've looked at using typed datasets but at the moment I cant find an example to learn from that's using the mysql connecter.

    Datasets aren't forward only, typed or untyped. Datareader is forward only.

    Never, ever, ever use an ordinal value to reference a column. Magic numbers are bad, baaaaaad.

    MyDataset.Tables("tablename").Rows(rownumber)("ColumnName")

    You might also want to RTFM.
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemDataDataSetClassTopic.asp


  • Closed Accounts Posts: 4,943 ✭✭✭Mutant_Fruit


    The only time you should ever use the ordinal number is when you do something like this...

    int StatusOrdinal=DataSet.Tables[0].Columns["Status"].Ordinal

    That stores the ordinal number of the column "status" in the "StatusOrdinal" int. You can then safely use the ordinal number (by using the "StatusOrdinal" int) in whatever way you choose. But every time your data changes, make sure you refresh the "index" int in case the columns change position for whatever reason.


Advertisement