Populate Textbox with data

fr0s1y

Member
Joined
May 16, 2011
Messages
14
Programming Experience
1-3
Hi,

I need to be able to fill a text box with data whenever someone selects a date in the monthly calendar. The Calendar reads the date from a database (im using mySql) and finds the corresponding data to fill, or at least thats how i want it to work!! heres what i have so far, what am i missing?

Private Sub dailyLogDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim myCommand As New MySqlCommand
Dim myAdapter As New MySqlDataAdapter
Dim myData As MySqlDataReader
Dim SQL As String


Dim _date = dailyLogDate.SelectionStart()

Dim notes = dailyLogTxt.Text()

myCommand.Connection = mySqlConn
SQL = "SELECT 'notes' FROM 'daily_log' WHERE _date = '" + _date + "'"
myCommand.Connection = mySqlConn
myCommand.CommandText = SQL
myAdapter.SelectCommand = myCommand
Try
mySqlConn.Open()
myData = myCommand.ExecuteReader()
myData.Read()
mySqlConn.Close()
If myData.HasRows = 0 Then
MsgBox("No Entries Today!.")
myData.Close()
Else
notes = myData.GetString("notes")
myData.Close()
Me.Close()
End If

Catch ex As MySqlException
MsgBox(ex.Message)

End Try



End Sub
 
I dont use mySQL to often, just some comments as i looked through your code. Maybe it might be of some help?

I've used SQL and from what i remember you only put the ' ' marks around what is suppose to be of a string value. like in sql a select statement would be

VB.NET:
SELECT columnName FROM tableName WHERE columnName='value'

it also looks like your missing a connection string and a mysqlconnection object.

check out this tutorial from VB/MySQL.com, it's pretty in depth and step by step, it should be of some help.

specifically look at part 4 where they go over data binding, and part 3 is more about the connection part of it. VB/MySQL.com » The VB.NET-MySQL Tutorial – Part 4

VB/MySQL.com » The VB.NET-MySQL Tutorial – Part 1 There should be a link at the bottom of the pages that links all 5 parts of this tutorial.



Hope that helps
 
Last edited:
Hi,

thanks for the reply, I've followed that tutorial loosely throughout the development of my application and i'm unsure as to any part that I can apply to my application.

That tut covers accessing/writing AND reading, all of which I can grasp and execute ok, its just populating an existing textbox is proving troublesome, that and getting it populated when a date is selected :confused:
 
Simple Example - working code, If this is what your talking about?

Not too knowledgeable on mySQL, if this doesn't help maybe somebody with more knowledge on here can

its just populating an existing textbox is proving troublesome, that and getting it populated when a date is selected
"Populating" a textbox? a textbox just holds a string value, so i am assuming you mean just that? I also didn't see what control you used in the code you posted previously for the calendar, so i made a quick sample using a month calendar control and a textbox.

As for populating the textbox when the date is selected just use the date changed event if you are using a month calendar control.

Here's some tested code. Hope it helps, if this is what your talking about?
Imports MySql.Data.MySqlClient

Public Class Form1
    Inherits System.Windows.Forms.Form


    Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles calSelectDate.DateChanged
        Dim selectedDate As String = e.Start.ToString("yyyy-MM-dd") 'if your getting the date from somewhere 
          'else just be sure to format it so mySQL understands it, again i am not sure what other formats work since im not 
          'familiar with mySQL but i know this format used here works fine.

        Dim cmd As New MySqlCommand
        Dim conn As New MySqlConnection()
        conn.ConnectionString = "Server=YOURSERVER;Database=YOURDB;Uid=YOUUSD;Pwd=YOUPWD;Encrypt=true;" 'remove encrypt if yours does not support/use

        Try
            conn.Open()

            cmd.Connection = conn
            cmd.CommandText = "SELECT Name FROM entries WHERE DateEntered = '" & selectedDate & "'"
                     'i used a select statement to get the info from the date selected                  

            Dim reader As MySqlDataReader = cmd.ExecuteReader
            Dim nameRcv As String = ""

            While reader.Read
                nameRcv = reader("Name") ' i just read it into a string, since it is only one value that is needed..
            End While

            If nameRcv <> "" Then
                txtName.Text = nameRcv ' set the text property of the textbox to the string retrieved from the db
            Else
                MsgBox("No Entries Today")
            End If

            conn.Close()

        Catch myerror As MySqlException
            MessageBox.Show("Error Connecting to Database: " & myerror.Message)
        Finally
            conn.Dispose()
        End Try
    End Sub


#Region "GENERATED"
    Sub New()
        InitializeComponent()
    End Sub

    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
    Private components As System.ComponentModel.IContainer
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.calSelectDate = New System.Windows.Forms.MonthCalendar()
        Me.txtName = New System.Windows.Forms.TextBox()
        Me.SuspendLayout()
        Me.calSelectDate.Location = New System.Drawing.Point(18, 18)
        Me.calSelectDate.Name = "calSelectDate"
        Me.calSelectDate.TabIndex = 0
        Me.txtName.Location = New System.Drawing.Point(18, 192)
        Me.txtName.Name = "txtName"
        Me.txtName.Size = New System.Drawing.Size(227, 20)
        Me.txtName.TabIndex = 1
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(261, 229)
        Me.Controls.Add(Me.txtName)
        Me.Controls.Add(Me.calSelectDate)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents calSelectDate As System.Windows.Forms.MonthCalendar
    Friend WithEvents txtName As System.Windows.Forms.TextBox
#End Region
End Class
 
Last edited:
Back
Top