.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> VB.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Retrieve all data from database using VB.net

Posted By: srini     Posted Date: November 16, 2010    Points:2   Category :VB.Net
Hello Everyone!

I am developing one application using vb.net and SQL server 2005. I am having Qusetion and answer column in database. I just wanted to retrieve all the data's(for ex:10 questions and answers)from database and show it into vb.net form having Labels control.

Help me!

Responses
Author: Asha.MP             
Posted Date: November 16, 2010     Points: 5   

Hi..

Considar this program, here DBConnect is defined in the web config as below.
<connectionStrings>
<add connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=E:\Extra\Develop\WindowsApplication1\WebApplication1\App_Data\Test.mdf;Integrated Security=True;User Instance=True" name="DBConnect" providerName ="System.Data.SqlClient"/>
</connectionStrings >
E:\Extra\Develop\WindowsApplication1\WebApplication1\App_Data\Test.mdf--> is the mdf file (DB)
Analyze this program,you will get the idea.
Dim conSetting As ConnectionStringSettings = System.Configuration.ConfigurationManager.ConnectionStrings("DBConnect")
Dim factory As DbProviderFactory = DbProviderFactories.GetFactory(conSetting.ProviderName)
Dim DbConnect As DbConnection = factory.CreateConnection()
Dim dt As New DataTable
DbConnect.ConnectionString = conSetting.ConnectionString
DbConnect.Open()
Dim cmdText As String = "select distinct(Place) from Company"
Dim cmd As DbCommand = DbConnect.CreateCommand()
cmd.CommandText = cmdText
Dim dbAdapter As DbDataAdapter = factory.CreateDataAdapter
dbAdapter.SelectCommand = cmd
dbAdapter.Fill(dt)
DbConnect.Close()
dbAdapter.Dispose()
DbConnect.Dispose()

Thanks and Regards,
Asha.mp
Author: srini             
Posted Date: November 16, 2010     Points: 5   

Asha,

Thanks for your code. I wanted to display all the table rows,while loading the form itself. In database,i am having questions and answers as two columns and it should be display in the respective form.

Help me!

Srini
Author: srini             
Posted Date: November 16, 2010     Points: 5   

Asha,

Thanks for your code. I wanted to display all the table rows,while loading the form itself. In database,i am having questions and answers as two columns and it should be display in the respective form.

Help me!

Srini
Author: srini             
Posted Date: November 17, 2010     Points: 5   

Hi,

This is my entire coding. In form load event,i am using "while loop" to display all the records in the table. But i found the code to display it in Msgbox instead of displaying in Labels. But its showing all the records in Msgbox. I need to diplay in Labels.

Code:

Imports System.Data.SqlClient
Public Class C
Inherits System.Windows.Forms.Form

Dim str_connection As String = "server=.\SQLExpress;Initial Catalog=test;Integrated Security=True"
Dim mycon As SqlConnection
Dim comUserSelect As SqlCommand
Dim dr As SqlDataReader
Dim ds As DataSet
Dim da As SqlDataAdapter
Dim dtFullDateFormat As Date
Dim intDuration As Integer = 0
Dim blnstart As String
Dim i As Integer


Private Sub C_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mycon = New SqlConnection(str_connection)
comUserSelect = New SqlCommand("SELECT * FROM Test ", mycon)
mycon.Open()
dr = comUserSelect.ExecuteReader

While dr.Read() 'While Data is Present
Label1.Text = dr.Item(0).ToString
Label2.Text = dr.Item(1).ToString
End While

'Show data in a Message Box
While dr.NextResult()
End While


Try
Dim MyHours As Integer = 1
Dim MyMinutes As Integer = 0
Dim MySeconds As Integer = 0
dtFullDateFormat = New DateTime(1, 1, 1, MyHours, MyMinutes, MySeconds)
intDuration = MyHours * 3600 + MyMinutes * 60 + MySeconds
txtTime.Text = dtFullDateFormat.ToString("HH:mm:ss")
Timer1.Interval = 1000
blnstart = True
Timer1.Start()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub



Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Static Ticks As Integer = 0
Try
If blnstart Then
Ticks = 0
blnstart = False
End If

If Ticks = intDuration Then
Timer1.Enabled = False
MessageBox.Show("Dude, Time is up! be ready to get your Results")
Application.Exit()
Else
Ticks += 1
txtTime.Text = DateAdd(DateInterval.Second, -Ticks, dtFullDateFormat).ToString("HH:mm:ss")
txtTime.Refresh()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub


End Class

Please Help me!!

Thanks,
Srini.

Srini


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend