Friends , I have a database in sql server named "Deposit" with a table "Receipts" .I want to display the data in a list box.My code is given below

Imports System.Data.SqlClient
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim cnn As New SqlConnection("Data Source = (Local);Initial Catalog = Deposit;Integrated Security = True")
Dim cmd As New SqlCommand("Select * from Receipts", cnn)
Dim rdr As SqlClient.SqlDataReader
cnn.Open()
rdr = cmd.ExecuteReader
While rdr.Read
ListBox1.Items.Add(rdr("Bank") & " " & rdr("Acno"))
End While
rdr.Close()
cnn.Close()
End Sub
End Class

But on running Nothing happens.The mistake is in the connection statement, I think. please correct me. My computer name is "BENNET-PC" and Database is in Sql server Management Studio

Dani AI

Generated

Most likely the problem is the server/instance part of the connection string rather than the code that reads rows. The exact text to put in Data Source= must match the "Server name" shown in SQL Server Management Studio's Connect dialog. Common cases:

  • a default instance uses the machine name (for example BENNET-PC)
  • SQL Server Express installs as a named instance (typically BENNET-PC\SQLEXPRESS)
  • LocalDB uses (localdb)\MSSQLLocalDB

As reported the machine name, try the precise server name shown by SSMS rather than (Local). Also note that Integrated Security=True uses the Windows account running the app — if that account has no DB permission, either grant it or switch to SQL authentication with User ID/Password.

Checklist of things to verify (fast):

  • Confirm the SQL Server service (and SQL Server Browser for named instances) is running.
  • In SSMS run SELECT TOP 5 * FROM Receipts to confirm the table and data exist.
  • Use the exact "Server name" string from SSMS in your connection string.
  • If the app gives no visible errors, catch exceptions (or run under the debugger) to see messages instead of silently failing.

Example connection-string patterns (replace instance/name as shown in SSMS):

Data Source=BENNET-PC;Initial Catalog=Deposit;Integrated Security=True
Data Source=BENNET-PC\SQLEXPRESS;Initial Catalog=Deposit;Integrated Security=True
Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Deposit;Integrated Security=True
Data Source=BENNET-PC\SQLEXPRESS;Initial Catalog=Deposit;User ID=sa;Password=YourPassword

A more robust populate approach is to fill a DataTable and bind a list of display strings to the ListBox (Using blocks + adapter so connections are always closed). Example (illustrative):

' Fill dt with SELECT Bank, Acno FROM Receipts then:
Dim display As New List(Of String)
For Each r As DataRow In dt.Rows
  display.Add(r("Bank").ToString() & " " & r("Acno").ToString())
Next
ListBox1.DataSource = display

Good pointer from about checking connection-string formats — copying the Server name from SSMS into one of the patterns above usually fixes this class of problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.