5 Replies - 1552 Views - Last Post: 04 March 2009 - 05:30 PM Rate Topic: -----

#1 rpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-March 09

connecting forms to sqlserver database using vb.net

Post icon  Posted 04 March 2009 - 03:30 PM

hi
i am new to dot net....m trying to access sqlserver management studio database using forms in vb.net...m using a button and i want to get access to the database when i click on that button....the code is given below....wen i tried the same code without using forms,this is working fine but it is not working when i use forms....

Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strcon As String = "Data Source=.\sqlexpress;Initial Catalog=employee;Integrated Security=True"
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader

con = New SqlConnection(strcon)
cmd = New SqlCommand("select * from employee", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
Console.Write(dr.Item(0))
Console.Write(dr.Item(1))
Console.Write(dr.Item(2))
End While
con.Close()
Console.ReadLine()
End Sub
End Class

m not getting any output using this code....please tell me where i went wrong...plz help me...it is urgent

Is This A Good Question/Topic? 0
  • +

Replies To: connecting forms to sqlserver database using vb.net

#2 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 53
  • View blog
  • Posts: 1,186
  • Joined: 05-June 08

Re: connecting forms to sqlserver database using vb.net

Posted 04 March 2009 - 04:47 PM

try this, add three textboxes to your form
and then change your code to this

textbox1.text = (dr.Item(0))
textbox1.text = (dr.Item(1))
textbox1.text = (dr.Item(2))



Was This Post Helpful? 0
  • +
  • -

#3 jackozi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 04-March 09

Re: connecting forms to sqlserver database using vb.net

Posted 04 March 2009 - 05:04 PM

View Postrpm, on 4 Mar, 2009 - 02:30 PM, said:

hi
i am new to dot net....m trying to access sqlserver management studio database using forms in vb.net...m using a button and i want to get access to the database when i click on that button....the code is given below....wen i tried the same code without using forms,this is working fine but it is not working when i use forms....

Imports System.Data.SqlClient

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strcon As String = "Data Source=.\sqlexpress;Initial Catalog=employee;Integrated Security=True"
Dim con As SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader

con = New SqlConnection(strcon)
cmd = New SqlCommand("select * from employee", con)
con.Open()
dr = cmd.ExecuteReader()
While dr.Read()
'Change those lines
Console.Write(dr.Item(0))
Console.Write(dr.Item(1))
Console.Write(dr.Item(2))
'so that you use ListBox1 control its on the toolBox
'with
ListBox1.Items.add(dr.Item(0) & " " & dr.Item(1) & " " & dr.Item(2))


End While
con.Close()
Console.ReadLine()
End Sub
End Class

m not getting any output using this code....please tell me where i went wrong...plz help me...it is urgent

Was This Post Helpful? 0
  • +
  • -

#4 rpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-March 09

Re: connecting forms to sqlserver database using vb.net

Posted 04 March 2009 - 05:15 PM

thx for replying me...

it worked but d problem now is that

the employee table contains 3 columns and 4 rows

once i used 3 textboxes, it is showing me only last row values...it is not the above ones....

wat shuld i do now??please reply
Was This Post Helpful? 0
  • +
  • -

#5 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 53
  • View blog
  • Posts: 1,186
  • Joined: 05-June 08

Re: connecting forms to sqlserver database using vb.net

Posted 04 March 2009 - 05:22 PM

well using the text boxes just showed that your code works.

if you want to display all the records you could have a look at something like what jackozi suggests or you could stick with the text boxes and add some functionality to navigate between records.

Check out the tutorial section, there are some there that should help you
Was This Post Helpful? 0
  • +
  • -

#6 rpm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 04-March 09

Re: connecting forms to sqlserver database using vb.net

Posted 04 March 2009 - 05:30 PM

@Damage and jackozi

thx a lot...
it worked now...:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1