Suggestion for storing selected radiobutton into database - database

I have create multiple choice system where the user need to choose answer from 4 radiobutton before navigate to next question by clicking next button.
So, the I have problem in storing the selected radiobutton into database. At first I create a table where I create columns to store each answer but it failed. Now, I'm stuck. Please give any suggestion where i can store the answer.
Here I provide the code to load the question
p/s: I know i should ask this on different thread but I also stuck on how to randomize the radiobutton.
Public Property Counter() As Integer
Get
Return IIf(ViewState("counter") Is Nothing, 1, CInt(ViewState("counter")))
End Get
Set(ByVal value As Integer
ViewState("counter") = value
End Set
End Property
Protected Sub Next_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Counter += 1
question()
clean()
End Sub
Sub question()
conn.Open()
Dim cmd As New SqlCommand("Select * From ques Where Id=#Id", conn)
cmd.Parameters.AddWithValue("#Id", Counter)
Dim dr1 As SqlDataReader
dr1 = cmd.ExecuteReader
If dr1.Read() Then
Me.lblquestion.Text = dr1("question")
Me.RadioButton1.Text = dr1("right")
Me.RadioButton2.Text = dr1("wrong")
Me.RadioButton3.Text = dr1("wrong2")
Me.RadioButton4.Text = dr1("wrong3")
Else
conn.Close()
Counter += 1
question()
End If
conn.Close()
End Sub
Sub clean()
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
Next.Enabled = False
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
question()
End Sub
Protected Sub RadioButton1_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
Next.Enabled = True
End Sub
Protected Sub RadioButton2_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
Next.Enabled = True
End Sub
Protected Sub RadioButton3_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
Next.Enabled = True
End Sub
Protected Sub RadioButton4_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
Next.Enabled = True
End Sub
End Class

I would have a database structure like this:
TblQuizes
Quiz_Id int
Quiz_Name nvarchar(200) -- or any other number that meets your demands
TblQuestions
Question_Id int
Question_Quiz_Id int --(FK To TblQuizes)
Question_Text nvarchar(200) -- or any other number that meets your demands
TblAnswers
Answer_Id int
Answer_Question_Id int --(FK To TblQuestions)
Answer_Text nvarchar(200) -- or any other number that meets your demands
Answer_IsCorrect bit
TblResults
Result_Id int
Result_Quiz_Id int -- (FK to TblQuizes) (not even needed, just to convenience)
-- Add user data in this table if you need it
TblResultDetails
ResultDetails_Id int
ResultDetails_Result_Id int --(FK to TblResults)
ResultDetails_Answer_Id int (FK to TblAnswers)
Now what I would do is create a new record in TblResults for every user that answers the quiz, and store the users answers in TblResultDetails.
On this basic structure you can create views to give you the entire data you need to show the quiz to the user, or to show him how well he answered your questions, and so on. you can also see very easily if a user did not complete the quiz, just by comparing the number of records in TblQuetions and TblResultDetails for a specific quiz id.
Also, if you ever want to have questions with only 2 answer, or 6 answers, you don't have to change anything in the database structure or in your code to have that.

Related

Search engine of my form does not return complete records when empty

I have a problem with the search engine of my form it does not return the complete records when it is empty it stays stuck with the last search instead of returning all the records.
This is my code
Private Sub txtBuscar_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtBuscar.TextChanged
Try
Dim cmd As New SqlCommand
Dim dtv As DataView
If txtBuscar.Text.Trim =
dtv = tb.Defaultview
dtv.RowFilter = "Material like'%" & txtBuscar.Text &"%'"
grdMateria.DataSource = dtv
grdMateria.Refresh()
Then Exit Sub
Then
Catch ex As Exception
End Try
End sub

How match system time with access database time?

I am creating an app in VB.NET that will play a song on user given time. User store song location and play time in ms-access database. when system time match with database time the song will play automatically. I've tried this with given below code but this is not working.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles Button1.Click
Me.Timer1.Start()
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
Call Play(Format(Now, "Long Time"))
End Sub
Sub Play(ByVal tm As String)
Dim ConnectionString As String
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=" & Application.StartupPath & "\VideoPlay.accdb;"
Dim AccessConnection As New
System.Data.OleDb.OleDbConnection(ConnectionString)
AccessConnection.Open()
Dim da As New System.Data.OleDb.OleDbDataAdapter("SELECT *
FROM VideoPlay", AccessConnection)
Dim ds As New DataSet
da.Fill(ds, "VideoPlay")
Dim dt As DataTable
dt = ds.Tables("VideoPlay")
Dim x As Integer
If dt.Rows.Count > 0 Then
For x = 0 To dt.Rows.Count - 1
If tm = dt.Rows(x).Item("TM") Then
MsgBox("yes")
End If
Next
End If
AccessConnection.Close()
da.Dispose()
ds.Dispose()
dt.Dispose()
AccessConnection.Close()
End Sub
End Class
Can any one tell me what is wrong in my code or any other way to compare system time with database time.
thanks in advance.

Visual Studio 2010 form creation: fill an array with contents of a combobox

Trying to knock out a project that's hanging my code without re-writing the entire thing. In it, I just need to populate an array with the information already in the Names combobox in order to proceed. As I'm not using .NET, my options are limited.
(The end result)*Add a button named “btnShowBalance” displaying the text “ShowBalance.” Write code in its event handler asking the user to enter a client’s name (InputBox). Search the names array for the name entered. If the name is found, use its location to retrieve the matching balance from the balances array. Display the client’s name and balance if the client exists; otherwise, display a not-found message. ****
Public Class Form1
Dim Balances(7) As Decimal
Dim Names(7) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboNames.Items.Add("John One")
cboNames.Items.Add("Jack Two")
cboNames.Items.Add("John Three")
cboNames.Items.Add("Jack Four")
cboNames.Items.Add("John Five")
cboNames.Items.Add("Jack Six")
cboNames.Items.Add("John Seven")
cboNames.Items.Add("Jack Eight")
cboBalances.Items.Add("235.50")
cboBalances.Items.Add("78943.98")
cboBalances.Items.Add("230781.10")
cboBalances.Items.Add("78362.00")
cboBalances.Items.Add("12097.20")
cboBalances.Items.Add("89267.34")
cboBalances.Items.Add("34959.06")
cboBalances.Items.Add("559284.50")
For i = 0 To cboNames.Items.Count - 1
Names(i) = cboNames.Text
Next
For i = 0 To cboBalances.Items.Count - 1
Names(i) = cboBalances.SelectedItem
Next
End Sub
Private Sub btnShowBalance_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowBalance.Click
Dim strResult As String
strResult = InputBox("Enter the name of the customer", "Balance Finder")
For i = 0 To Names.Length - 1
If strResult = Names(i) Then
MessageBox.Show(Names(i).ToString & " " & Balances(i).ToString)
End If
Next
End Sub
End Class

Need help searching an array... very hard for me

Got a small problem. I don't know how to search one array so I can pull that same array number from the other 2 arrays. I know how to test for a lot of stuff so that won't be a problem.
The end result on this project is the user will place the amount they are willing to pay for a make of the car and the page will display the data. HOWEVER I don't know how to search the carArray() to find the index number and use that index number to find the other stuff. I did find something that did this (somewhat) earlier but I don't know how to modify it for me to keep that index number as a int and use it to search and display the other arrays.
I will need this in future projects later.
Public Class paymentPage
Private Sub car_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles car.TextChanged
End Sub
Private Sub price_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles price.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim carArray() As String = {"Ford", "Chevy", "Mazda"}
Dim sellAmount() As Decimal = {32700, 35625, 24780}
Dim leaseAmount() As Decimal = {425, 505, 385}
End Sub
End Class
Why not make this a class object? Easier to reuse later.
Public Class Car
Public Property Make As String
Public Property Value As Double
Public Property Lease As Double
End Class
Then make a collection of them:
Private cars As New List(Of Car)
cars.Add(New Car With {.Make = "Ford", .Value = 32700, .Lease = 425})
cars.Add(New Car With {.Make = "Chevy", .Value = 35625, .Lease = 505})
cars.Add(New Car With {.Make = "Mazda", .Value = 24780, .Lease = 385})
For your requirements:
Private Function getIndexByName(make As string) As Integer
Dim result As Integer = -1
For i As Integer = 0 To carArray.Length -1
If carArray(i) = make Then
result = i
Exit for
End If
Next
Return Result
End Function
Usage:
Dim mazdalease = leaseAmt(getIndexByName("Mazda"))
Dim cars as new List(Of Car)({car1,car2,car3})
Dim indexOfCar2 = Array.IndexOf(cars.ToArray(),car2)
Since its dirt simple to convert to an array then you can use the built in function. Keep in mind that you need to override GetHash and Equals to get this to work properly.

UI not updating fast

Good evening,
Following is the code I used for reading the files and folders from a drive etc.
Public Class LoadingBox
Public counter As ULong
Public OpenRecords As New Dictionary(Of String, MainWindow.records)
Public Path As String
Public Diskname As String
Private WithEvents BKWorker As New BackgroundWorker()
Public Sub New(ByVal _Path As String, ByVal _Diskname As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Path = _path
Diskname = _diskname
End Sub
Private Sub GetStructure(ByVal tempdir As String, ByVal ParentID As String, ByVal DiskName As String)
Dim maindir As DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(tempdir)
For Each Dir As DirectoryInfo In maindir.GetDirectories
Try
Dim d As New MainWindow.records
d.Filename = Dir.Name
d.Folder = True
d.Rowid = Date.UtcNow.ToString() + counter.ToString()
d.Size = 0
d.ParentID = ParentID
d.DiskName = DiskName
d.DateCreated = Dir.CreationTimeUtc
d.DateModified = Dir.LastWriteTimeUtc
OpenRecords.Add(d.Rowid, d)
'Label1.Content = "Processing: " + Dir.FullName
BKWorker.ReportProgress(0, Dir.FullName)
counter = counter + 1
GetStructure(Dir.FullName, d.Rowid, DiskName)
Catch ex As Exception
End Try
Next
For Each fil As FileInfo In maindir.GetFiles
Try
Dim d As New MainWindow.records
d.Filename = fil.Name
d.Folder = False
d.Rowid = Date.UtcNow.ToString() + counter.ToString()
d.Size = fil.Length
d.ParentID = ParentID
d.DiskName = DiskName
d.DateCreated = fil.CreationTimeUtc
d.DateModified = fil.LastWriteTimeUtc
OpenRecords.Add(d.Rowid, d)
'Label1.Content = "Processing: " + fil.FullName
BKWorker.ReportProgress(0, fil.FullName)
counter = counter + 1
Catch ex As Exception
End Try
Next
End Sub
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
counter = 0
BKWorker.WorkerReportsProgress = True
AddHandler BKWorker.DoWork, AddressOf BKWorker_Do
AddHandler BKWorker.ProgressChanged, AddressOf BKWorker_Progress
AddHandler BKWorker.RunWorkerCompleted, AddressOf BKWorker_Completed
BKWorker.RunWorkerAsync()
'GetStructure(Path, "0", Diskname)
End Sub
Private Sub BKWorker_Do(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Throw New NotImplementedException
GetStructure(Path, "0", Diskname)
End Sub
Private Sub BKWorker_Progress(ByVal sender As Object, ByVal e As ProgressChangedEventArgs)
'Throw New NotImplementedException
Label1.Content = "Processing: " + e.UserState.ToString()
If ProgressBar1.Value = 100 Then
ProgressBar1.Value = 0
End If
ProgressBar1.Value = ProgressBar1.Value + 1
End Sub
Private Sub BKWorker_Completed(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
'Throw New NotImplementedException
MessageBox.Show("Completed")
Me.Close()
End Sub
End Class
However the problem is that, the background thread is able to read files very fast, but the UI thread is not able to keep up the speed with it, could you please advice me on how I can solve this issue.
You almost never want to report progress on every single item when you're iterating through that many items.
I would suggest finding some reasonable number of files to wait for before reporting progress. Every 5th or every 10th or so on. You probably want to take a look at what your normal number of files is. In other words, if you're normally processing only 25 files, you probably don't want to only update every 10 files. But if you're normally processing 25000 files, you could maybe even only update every 100 files.
One quick answer would be to only report the progress when a certain amount of time has passed that way if 10 files were processed in that time the UI isn't trying to update one each one. If things are processing that fast then you really don't need to update the user on every single file.
Also on a quick side note if your ProgressBar isn't actually reporting progress from 0 to 100% you might want to just set its IsIndeterminate property to true instead of increasing the percent and then resetting it back to 0.

Resources