By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 664 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

How do I get data from SQL database to my dynamic textboxes VB.NET

P: 2
Hello guys/girl, how are You. I have one problem. I'm creating a exam system which should give 40 random questions from database table(mySQL). I managed to create (not perfect, still small problems with .possition and .size ) textboxes and checkboxex ( on button click event ). And I managed to get ONE (first row) data from my sql but then it stops. I cannot create more textboxes or checkboxex. See my code below. If You didnt fully understand what I want or what I need, please ask question and I'll give the asnwer.

CODE IN COMMENT ( SPAM ERROR )
May 30 '17 #1
Share this Question
Share on Google+
1 Reply


P: 2
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Public Class Form1
  3.     Dim cn As New SqlConnection("Data Source=192.168.0.45,1433;Network Library=DBMSSOCN;Initial Catalog=DB_GS-TMT_SSuite;User ID=itmahir;Password=itadmin;")
  4.     Private m_textboxes() As TextBox = {}
  5.     Private m_checkboxes() As CheckBox = {}
  6.     Dim i As Integer = m_textboxes.Length
  7.     Dim a As Integer = m_checkboxes.Length
  8.     Public Overrides Property AutoScroll As Boolean
  9.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  10. *
  11.         ReDim Preserve m_textboxes(i)
  12.         m_textboxes(i) = New TextBox
  13.         With m_textboxes(i)
  14.             .Name = "TextBox" & i.ToString
  15.             .Text = "TextBox" & i.ToString
  16.             If m_textboxes.Length < 2 Then
  17.                 .SetBounds(10, 10, 100, 20)
  18.             Else
  19.                 .Left = m_textboxes(i - 1).Left
  20.                 .Top = m_textboxes(i - 1).Top + m_textboxes(i - 1).Height + 4
  21.                 .Size = m_textboxes(i - 1).Size
  22. *
  23.             End If
  24.             .Tag = i
  25. *
  26.         End With
  27.         AddHandler m_textboxes(i).TextChanged, AddressOf TextBox_TextChanged
  28.         Me.Controls.Add(m_textboxes(i))
  29.         CitacBaze()
  30.         ReDim Preserve m_checkboxes(a)
  31. *
  32.         m_checkboxes(a) = New CheckBox
  33.         With m_checkboxes(a)
  34.             .Name = "CheckBox" & a.ToString
  35.             .Text = "CheckBox" & a.ToString
  36.             If m_checkboxes.Length < 2 Then
  37.                 .SetBounds(135, 10, 100, 20)
  38.             Else
  39.                 .RightToLeft = m_checkboxes(a - 1).RightToLeft
  40.                 .Top = m_checkboxes(a - 1).Top + m_checkboxes(a - 1).Height + 4
  41.                 .Size = New Size(20, 20)
  42. *
  43.             End If
  44.             .Tag = a
  45.         End With
  46.         AddHandler m_checkboxes(a).CheckStateChanged, AddressOf CheckBox_CheckStateChanged
  47.         CitacOdgovora()
  48.         Me.Controls.Add(m_checkboxes(a))
  49. *
  50.     End Sub
  51.     Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  52.         Dim txt As TextBox = DirectCast(sender, TextBox)
  53.         Debug.WriteLine(txt.Name & ": [" & txt.Text & "]")
  54.     End Sub
  55.     Private Sub CheckBox_CheckStateChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
  56.         Dim chB As CheckBox = DirectCast(sender, CheckBox)
  57.         Debug.WriteLine(chB.Name & ": [" & chB.Text & "]")
  58.     End Sub
  59.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  60. *
  61.         Me.AutoScroll = True
  62. *
  63.  
  64.     End Sub
  65.     Public Sub CitacBaze()
  66.         cn.Open()
  67.         Dim cmd As New SqlCommand
  68.         Dim dadapter As New SqlDataAdapter
  69.         Dim datardr As SqlDataReader
  70.         Dim strSql As String
  71. *
  72.         strSql = "SELECT * FROM TBL_Testiranje_Radnik"
  73.         cmd.CommandText = strSql
  74.         cmd.Connection = cn
  75.         dadapter.SelectCommand = cmd
  76.         datardr = cmd.ExecuteReader
  77.         If datardr.HasRows Then
  78.             datardr.Read()
  79.             With m_textboxes(i)
  80.                 .Text = datardr("TekstPitanje")
  81. *
  82.             End With
  83. *
  84.         End If
  85.         cn.Close()
  86. *
  87.  
  88.     End Sub
  89.     Public Sub CitacOdgovora()
  90.         cn.Open()
  91.         Dim cmd As New SqlCommand
  92.         Dim dadapter As New SqlDataAdapter
  93.         Dim datardr As SqlDataReader
  94.         Dim strSql As String
  95. *
  96.         strSql = "SELECT * FROM TBL_Testiranje_Radnik"
  97.         cmd.CommandText = strSql
  98.         cmd.Connection = cn
  99.         dadapter.SelectCommand = cmd
  100.         datardr = cmd.ExecuteReader
  101.         If datardr.HasRows Then
  102.             datardr.Read()
  103. *
  104.  
  105.             With m_checkboxes(a)
  106.                 .Text = datardr("OdgovorA")
  107.             End With
  108. *
  109.         End If
  110.         cn.Close()
  111. *
  112.  
  113.     End Sub
  114.     Public Sub KreiranjeTextBox()
  115.         ReDim Preserve m_textboxes(i)
  116.         m_textboxes(i) = New TextBox
  117.         With m_textboxes(i)
  118.             .Name = "TextBox" & i.ToString
  119.             .Text = "TextBox" & i.ToString
  120.             If m_textboxes.Length < 2 Then
  121.                 .SetBounds(10, 10, 100, 20)
  122.             Else
  123.                 .Left = m_textboxes(i - 1).Left
  124.                 .Top = m_textboxes(i - 1).Top + m_textboxes(i - 1).Height + 4
  125.                 .Size = m_textboxes(i - 1).Size
  126. *
  127.             End If
  128.             .Tag = i
  129. *
  130.         End With
  131.         AddHandler m_textboxes(i).TextChanged, AddressOf TextBox_TextChanged
  132.         Me.Controls.Add(m_textboxes(i))
  133.         CitacBaze()
  134.     End Sub
  135.     Public Sub KreiranjeCheckBoxova()
  136.         ReDim Preserve m_checkboxes(a)
  137. *
  138.         m_checkboxes(a) = New CheckBox
  139.         With m_checkboxes(a)
  140.             .Name = "CheckBox" & a.ToString
  141.             .Text = "CheckBox" & a.ToString
  142.             If m_checkboxes.Length < 2 Then
  143.                 .SetBounds(135, 10, 100, 20)
  144.             Else
  145.                 .RightToLeft = m_checkboxes(a - 1).RightToLeft
  146.                 .Top = m_checkboxes(a - 1).Top + m_checkboxes(a - 1).Height + 4
  147.                 .Size = New Size(20, 20)
  148. *
  149.             End If
  150.             .Tag = a
  151.         End With
  152.         AddHandler m_checkboxes(a).CheckStateChanged, AddressOf CheckBox_CheckStateChanged
  153.         CitacOdgovora()
  154.         Me.Controls.Add(m_checkboxes(a))
  155. *
  156.     End Sub
  157. End Class
  158.  
May 30 '17 #2

Post your reply

Sign in to post your reply or Sign up for a free account.