Insert Record to SQL is not working

EMI-IT

New member
Joined
Nov 18, 2006
Messages
1
Programming Experience
3-5
Hi everybody,

I tried to developed a windows application which is works with SQL Server to manipulate data. The problem is when I tried to insert a record to the db it doesn't work, I mean it doesn't give any error but doesn't insert any data in the ql server. :confused:

here is my code:

VB.NET:
PrivateSub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
'This is a call to updateRecord Fuction
Me.UpdateRecord("INSERT INTO customers(SN,customerid,customername,companyname,companyaddress,phone,fax)VALUES(" & number & ",'" & customeridtxt.Text & "','" & customernametxt.Text & "','" & companytxt.Text & "','" & addresstxt.Text & "','" & phonetxt.Text & "','" & faxtxt.Text & "');")
EndSub
 
'Here is the UpdateRecord Function
PublicFunction UpdateRecord(ByVal querystr AsString)
Dim updatecmd AsNew SqlCommand
Dim con As SqlConnection = New SqlConnection(g.GetCon)
Dim da AsNew SqlDataAdapter
Try
con.Open()
Catch ex As Exception
MsgBox(ex.Message)
EndTry
Try
updatecmd.Connection = con
updatecmd.CommandText = querystr
updatecmd.ExecuteNonQuery()
Catch ex As Exception
MsgBox(ex.Message)
EndTry
MsgBox("Information has been saved successfully", MsgBoxStyle.Information, "Confirmation")
con.Close()
ReturnTrue
EndFunction

waiting ur reply :)


Thank you.
 
Last edited by a moderator:
Given that youre using .NET 2.0, you shouldnt really be doing data access like this at all.. its very old fashioned and insecure. Take a read of the DW2 link in my signature. Good luck!
 
If you want to use the controls to preview the data before comit to the DB, then using data bound controls (I thought) wouldn't do the job.
I am just starting with VB & SQL server, (not used VB before!) I have loads of text boxes on a form, which are unbound. They can be used to search, edit or create new records. If they were bound controls, as soon as you alter the data in a control, it is commited to the DB (you are going to say I'm wrong - I just know it!)
So .. lets say you have a list of employees - the first text box might have the employee ID, so you could use the form to search (by clicking on search button) when there is data in that text box, or create a new record .. etc.

I sense a re-write coming on??
 
You're right.... I am going to say you're wrong. Properly built databound controls in .NET do not directly update the database. That's because ADO.NET works in a disconnected state. What does get updated is the bound object. I say object because it's possible to bind to a Datatable, a dataset, or a custom class.

Personaly I think it's a good idea to separate the search from the new/edit capabilities. But that's just me.

-tg
 
If you want to use the controls to preview the data before comit to the DB, then using data bound controls (I thought) wouldn't do the job.
I am just starting with VB & SQL server, (not used VB before!) I have loads of text boxes on a form, which are unbound. They can be used to search, edit or create new records. If they were bound controls, as soon as you alter the data in a control, it is commited to the DB (you are going to say I'm wrong - I just know it!)


As TG notes, there is no direct link to the DB.. you download stuff into the client, edit it and maybe send it back. As soon as you alter data in a control, it changes the Current version of the data row member to the shown value


Say we have a data row with jsut one column, Name
It contains the value Fred, and it has jsut been d/l from a database.
Both the Current and Original version of this cell say Fred
Your textbox shows Fred
You edit the TB to say Joe and now we have a cell that:
Original version says Fred
Current says Joe


This is maybe accessory to your question, but it may help you understand how it is possible to say RejectChanges on a datatable and have all edited values revert to their previous state
 
Back
Top