Connectivity of SQL 2000 + VB using ODBC

Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #21
Apr 15th, 2008
how do you expect this code to run? the first statement contains a big error. apart from this, the insert statement which you have written & passed to the variable query contains syntax errors. errors due to nontermination of string constant and invalid sql command. the following is the corrected syntax. try this...

Dim gcn As New ADODB.Connection
Dim query As String

gcn.ConnectionString="Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
gcn.Open
Msgbox "An active connection has been established."

query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close

''your insert statement should look like this :-
INSERT INTO STUINFO(ROLL,NAME,MARKS) VALUES('1','SONIA',100)

regards
Shouvik
Last edited by choudhuryshouvi; Apr 15th, 2008 at 1:34 am.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #22
Apr 15th, 2008
Hi Shouvik ,
query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"

1)If we broke the above line into two lines y the error comes. To split the above line what I do??

2)Second thing when i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.

3) Third thing,roll no is integer na then y u use single quotes as below-
'" & txtroll.text & "'

4)Fourth thing,Record is inserted as below-
roll name marks
0 blank 0
0 blank 0

5)This method of connectiong to the database is ADO.rite?
Last edited by sonia sardana; Apr 15th, 2008 at 7:17 am.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #23
Apr 16th, 2008
ok......sonia......see your answers inline.

query="insert into stuinfo(roll,name,marks) values('" & txtroll.text & "','" & txtname.text & "'," & val(txtmarks.text) & ")"

1)If we broke the above line into two lines y the error comes. To split the above line what I do??
to break the line use this syntax :-
query = "insert into stuinfo(roll,name,marks)" & _
           "values('" & txtroll.Text & "','" & _
            txtname.Text & "'," & Val(txtmarks.Text) & ")"
see the Bolded part. it is the syntax (an ampersand followed by a space and an underscore) for breaking the line. this process is called Line Continuation.

2)Second thing when i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.
sounds confusing!!! would you mind in explaining this a little bit clear?

3) Third thing,roll no is integer na then y u use single quotes as below-
'" & txtroll.text & "'
i was just assumed that. there is no hard-n-fast rule that roll no becomes always integer. it can be alphanumeric like 'R001'. so to show you an example i used that one. you have to change it accordingly based on your data type. if its an integer avoid that single quotes otherwise use those.

4)Fourth thing,Record is inserted as below-
roll name marks
0 blank 0
0 blank 0
well I am unable to speak a single word without see what have you done so far. because the statement i used here is absolutely correct. now if its not so problem for you would you mind in posting your code for inserting record here?

5)This method of connectiong to the database is ADO.rite?
yes, you are 1000% correct.

regards
Shouvik
Last edited by choudhuryshouvi; Apr 16th, 2008 at 2:01 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #24
Apr 16th, 2008
Hey Shouvik,I m trying to insert the records into the DATABASE Table.

Form Designing-Three textboxes , & one Insert button.

I m inserting just numbers in the column roll.

DATABASE TABLE-create table stuinfo(roll int,name varchar(10),marks int)
select * from stuinfo

I m writing the below mentioned coding-
How Can I explain U-
When i run the form,Msg comes Record inserted and when i click on ok then the form comes. And when we fill the fields & click on OK button,then the msg do not come.

U plz Run this coding by urself,then u know what I m trying to say.


Private Sub Form_Load()
Dim gcn As New ADODB.Connection
Dim query As String
gcn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=sonia"
gcn.Open
query = "insert into stuinfo(roll,name,marks) values(" & Val(txtroll.Text) & ",'" & txtname.Text & "'," & Val(txtmarks.Text) & ")"
gcn.Execute (query)
MsgBox "Record Inserted"
gcn.Close
End Sub

roll name marks
0 blank 0
0 blank 0

By saying this i m trying to say that,The records that are inserted into the database are
Always 0 is inserted for roll & marks,
& Blank is inserted for name.

I hope U Undersyand What I m trying to say.
hey one more thing tell me that Mastering in Visual basic 6.0 is good for freshers nor not.
It is not too highfy book na, dat freshers like me can’t understand. Cz I m planning to purchase dat book.
Last edited by sonia sardana; Apr 16th, 2008 at 4:58 pm.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #25
Apr 17th, 2008
ok......this is the reason for what happening to you...

first, you have placed the record insertion code in wrong section. you are firing that code in form_load event. in this event vb actually initializes all controls. so at this time your three textboxes contain null values like txtroll="" , txtname="" and txtmarks="" . now when your program fires the
Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. gcn.execute "insert..."
statement, it finds nothing in the text boxes. now as per your code it is converting value of those text boxes like ,
val(txtroll.text) --> 0
txtname.text --> null
val(txtmarks.text) --> 0
and storing them in your table.
as you already known that val converts equivalent string to numbers. so when it finds a null value it passes only 0. now all your statements are executing fine coz there is no syntax or compilation errors. so the msgbox "Record Inserted" is being displayed. your code has some logical mistakes.

now you said
when click on the insert button the msgbox is not coming
.
this might has the following causes :-
1. you didn't mention the insertion code under the button.
2. though you have coded it but the msgbox statement is not there.

you have to code your record insertion statements in both of your form_load (sounds crazy but as you told) and button_click events so that the same action can be performed by your program when you wish to do so. now you have written in the code in form_load event. that's why the msgbox is coming when start debugging your program and not coming when you click on the button because clicking on the button generates a new separate event which has no relation with the form_load event--->means the form will not be loaded once again after clicking on the button.

hope you will be understood.

now for your consideration i have written the following code using the same database,provider,table,fields and form design. also there is a screenshot for you.
check this out and inform me what have you got...

general declaration section. global variable
Dim gCn As New ADODB.Connection

'will insert a record into your table
Private Sub cmdinsert_Click()
If Trim(txtroll.Text) = "" Then
    MsgBox "Please input rollno."
    txtroll.SetFocus
    Exit Sub
End If

If Trim(txtname.Text) = "" Then
    MsgBox "Please input name."
    txtname.SetFocus
    Exit Sub
End If

If Trim(txtmarks.Text) = "" Then
    MsgBox "Please input marks."
    txtmarks.SetFocus
    Exit Sub
End If

Dim confirm As Integer

confirm = MsgBox("Sure to add this record?", vbYesNo)
If confirm = vbNo Then
    txtroll.SetFocus
Else
    gCn.Execute "insert into stuinfo(roll,name,marks) values(" & _
        Val(Trim(txtroll.Text)) & ",'" & Trim(txtname.Text) & "'," & _
        Val(Trim(txtmarks.Text)) & ")"
    If gCn.Errors.Count = 0 Then
        Call DisplayAllStudents '--->''calling the sub-routine so that   you can ensure the new record has been added.
        
        MsgBox "New student added."
        txtroll.Text = ""
        txtname.Text = ""
        txtmarks.Text = ""
        txtroll.SetFocus
    End If
End If
End Sub

just creating connection with the database and nothing else.
avoid putting any DML statements here
Private Sub Form_Load()
gCn.ConnectionString = "Provider=SQLOLEDB.1;Persist Security Info=False;" & _
                       "User ID=sa;Initial Catalog=sonia"
gCn.Open

Call DisplayAllStudents
End Sub

take a listview control. for this select Microsoft Common Controls 6.0 (SP3) from components list. you can access it via Project->Components
it will fetch all existing student records from your table
Public Sub DisplayAllStudents()
Dim str As String
Dim rs As New ADODB.Recordset
Dim li As ListItem

str = "select * from stuinfo order by roll"
rs.CursorLocation = adUseClient
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
rs.Open str, gCn

lvstudents.ListItems.Clear

If rs.RecordCount > 0 Then
    rs.MoveFirst
    While Not rs.EOF()
        With lvstudents
            Set li = .ListItems.Add(, , (rs!roll))
            li.SubItems(1) = rs!Name
            li.SubItems(2) = rs!marks
        End With
        rs.MoveNext
    Wend
End If

If rs.State = adStateOpen Then rs.Close
Set rs = Nothing
End Sub

regards
Shouvik
Last edited by choudhuryshouvi; Apr 17th, 2008 at 3:10 am.
Attached Thumbnails
Proof.JPG  
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #26
Apr 17th, 2008
Originally Posted by sonia sardana View Post
hey one more thing tell me that Mastering in Visual basic 6.0 is good for freshers nor not.
It is not too highfy book na, dat freshers like me can’t understand. Cz I m planning to purchase dat book.
ya, that's a good book. i have an electronic copy of this book too. go and purchase it.
it will help you. but i think it has some sections where explanations are a little bit advanced for freshers.

hv a nice day.

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #27
Apr 17th, 2008
hey Shouvik ,Till Now I have did the foll--
I inserted the records and as the records are inserted into the database,all the textboxes are clear.
THX FOR UR HELP.
Now I m trying to do myself DELETION, UPDATION, SEARCHING the Records.

The eg,u have given me in ur previous reply is
GRIDVIEW DATABINDING--Rite????


Till Now,I understood that
U write Connection string as procedure.
When the User clicks on Insert button further msg box is displayed.
If yes,Records are inserted.
If no,Focus is set to textbox Roll.

And To Clear The textboxes I did-
Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next
End Sub
Last edited by sonia sardana; Apr 17th, 2008 at 7:40 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: sonia sardana has a little shameless behaviour in the past 
Solved Threads: 7
sonia sardana sonia sardana is offline Offline
Posting Whiz

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #28
Apr 17th, 2008
hey Shouvik,Till Now i do not want GRIDVIEW DATABINDING, I am doing if user types Rollno 1 in textbox Roll and click on search button, then in the corresponding textboxes info is displayed in other textboxes like his name,& his marks.
In VB there is no DataReader.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #29
Apr 17th, 2008
hey Shouvik ,Till Now I have did the foll--
I inserted the records and as the records are inserted into the database,all the textboxes are clear.
THX FOR UR HELP.
Now I m trying to do myself DELETION, UPDATION, SEARCHING the Records.
that's a good news. so you are on your way. isn't it?
ok...try those and if you need any help then plz create a new thread for that.

The eg,u have given me in ur previous reply is
GRIDVIEW DATABINDING--Rite????
wrong. there is no gridview databinding concept available in vb6. it was just a listview control and it is showing all existing records from your table. look in your VS2005 toolbox......you'll find this control there too...

Till Now,I understood that
U write Connection string as procedure.
When the User clicks on Insert button further msg box is displayed.
If yes,Records are inserted.
If no,Focus is set to textbox Roll.
wrong. i didn't write connection string as procedure. the connection string is simple a method and i just initialized it by putting my server descriptions and used open method to create the connection. the only procedure in that code is DisplayAllStudents which is responsible for fetching & displaying all existing student records from your stuinfo table.
the msgbox's purpose is exactly same what you have said.

And To Clear The textboxes I did-
Public Sub clear()
Dim txt As Control
For Each txt In Controls
If TypeOf txt Is TextBox Then
txt.Text = ""
End If
Next
End Sub
ok....it is 1000% correct. there is no error in it.

so what is the summery?
now you are successfully adding your data into the database or not?
have my efforts showed you some ways or not???

reply...

regards
Shouvik
Last edited by choudhuryshouvi; Apr 17th, 2008 at 2:43 pm.
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 537
Reputation: choudhuryshouvi is an unknown quantity at this point 
Solved Threads: 49
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro

Re: Connectivity of SQL 2000 + VB using ODBC

 
0
  #30
Apr 17th, 2008
Originally Posted by sonia sardana View Post
hey Shouvik,Till Now i do not want GRIDVIEW DATABINDING, I am doing if user types Rollno 1 in textbox Roll and click on search button, then in the corresponding textboxes info is displayed in other textboxes like his name,& his marks.
In VB there is no DataReader.
yes, you are right. in vb6 there is no concept of datareader.
but so what? there are still so many methods that you can use to read your records from the database.

so you wish to try the search code yourself?
ok.....go ahead....i'm always here for you.

for searching you can apply one of the these two techniques :-
1. Exact Record Searching
2. Dictionary Searching

choose which will be best suited for you.

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum
Thread Tools Search this Thread



Tag cloud for Visual Basic 4 / 5 / 6
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC