|
-
Jun 20th, 2004, 02:44 PM
#1
Thread Starter
Lively Member
(resolved)***Adding data to sql table
Hello everyone,
I have a test box (txtAdd) and a button (btnAdd) on a form. I want to type in a number in the text box, click the add button, and have it save to my sql database. I have the sql statements but im not sure what the other code looks like that I need. Could some show me an example?
Thanks
Last edited by pea33nut; Jun 28th, 2004 at 01:37 PM.
-
Jun 20th, 2004, 06:16 PM
#2
Thread Starter
Lively Member
-
Jun 20th, 2004, 08:25 PM
#3
Registered User
Re: Adding data to sql table
Originally posted by pea33nut
Hello everyone,
I have a test box (txtAdd) and a button (btnAdd) on a form. I want to type in a number in the text box, click the add button, and have it save to my sql database. I have the sql statements but im not sure what the other code looks like that I need. Could some show me an example?
Thanks
this from mssql 2000
VB Code:
@txtbox varchar (50)
as
insert into nameoftable
values(@txtbox)
go
hope it helps
-
Jun 20th, 2004, 08:43 PM
#4
Thread Starter
Lively Member
I think I miss wrote what I wanted. I have that part of the code but I need to know what vb code is needed to send it to the data base. Do I need the Dim cmdtext As String? Or anything in that nature. This is some code I use to retrieve data from the database.
da.Fill(myDS, "Auto")
With cboItemNumber
.DataSource = myDS.Tables("Auto")
.DisplayMember = "ItemNumber"
So is thier any vb code to add my sql statement in?
thanks
-
Jun 20th, 2004, 09:19 PM
#5
Registered User
Originally posted by pea33nut
I think I miss wrote what I wanted. I have that part of the code but I need to know what vb code is needed to send it to the data base. Do I need the Dim cmdtext As String? Or anything in that nature. This is some code I use to retrieve data from the database.
da.Fill(myDS, "Auto")
With cboItemNumber
.DataSource = myDS.Tables("Auto")
.DisplayMember = "ItemNumber"
So is thier any vb code to add my sql statement in?
thanks
VB Code:
Function add()
Try
With cm
.Parameters.Clear()
.CommandText = "addtable"
.Parameters.Add("@name", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
.Parameters.Add("@address", SqlDbType.VarChar, 50).Direction = ParameterDirection.Input
.Parameters(0).Value = txtbox1.text
.Parameters(1).Value = txtbox2.text
.ExecuteNonQuery()
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function
this what i do to save in my database (ms sql 200)
addtable is a storedprocedure
hope it helps..
-
Jun 20th, 2004, 09:38 PM
#6
Fanatic Member
one other way.
VB Code:
Dim cn As New SqlConnection()
cn.ConnectionString = "integrated security=true;initial catalog=tempdb"
cn.Open()
Dim cmdtext As String = "insert into table1 values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
Dim cm As New SqlCommand(cmdtext, cn)
cm.ExecuteNonQuery()
cn.Close()
-
Jun 21st, 2004, 01:43 AM
#7
Registered User
brown monkey code is quiet good than mine..
im just a stupid beginner....
-
Jun 27th, 2004, 08:37 PM
#8
Thread Starter
Lively Member
What does the cm.ExecuteNonQuery() do? I got and error when I ran my program that said
(An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll)
Additional information: System error.
-
Jun 27th, 2004, 08:51 PM
#9
Fanatic Member
that's [sql | oledb | odbc | etc...]command.executequery executes a non query. sample, update, delete. i don't know exactly about your error but i think you have no database named tempdb and table in tempdb named table1. you should make this. hope this helps.
-
Jun 27th, 2004, 09:13 PM
#10
Thread Starter
Lively Member
Here is my code.
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Connect to the database
Dim cn As SqlConnection = New SqlConnection
cn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Justin;" & _
"Integrated Security=SSPI"
cn.Open()
' Add Data to the database
Dim cmdtext As String = "insert into Auto values('" & txtAddItemNumber.Text & "',')"
Dim cm As New SqlCommand(cmdtext, cn)
cm.ExecuteNonQuery()
cn.Close()
The cm.executenonquery() is highlighted green with this error.
****An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll
Additional information: System error.
-
Jun 27th, 2004, 09:16 PM
#11
Fanatic Member
perhaps "insert into [auto]..." not "auto" alone. auto is a reserved word...
-
Jun 27th, 2004, 09:49 PM
#12
Thread Starter
Lively Member
Hmmm, good thought but still no luck.
-
Jun 27th, 2004, 09:51 PM
#13
Fanatic Member
Originally posted by pea33nut
Dim cmdtext As String = "insert into Auto values('" & txtAddItemNumber.Text & "',')"
what's with the comma?
-
Jun 28th, 2004, 07:21 AM
#14
Thread Starter
Lively Member
I took out the comma but I still get the same error.
' Connect to the database
Dim cn As SqlConnection = New SqlConnection
cn.ConnectionString = "Data Source=(local);" & _
"Initial Catalog=Justin;" & _
"Integrated Security=SSPI"
cn.Open()
' Add Data to the database
Dim cmdtext As String = "insert into [Auto] values('" & txtAddItemNumber.Text & "')"
Dim cm As New SqlCommand(cmdtext, cn)
cm.ExecuteNonQuery()
cn.Close()
thier seems to be something wrong with the cm.executenonquery()
-
Jun 28th, 2004, 08:24 AM
#15
Thread Starter
Lively Member
Auto is the table but ItemNumber is the field that I want the txtAddItemNumber value to go. Shouldn't I have the ItemNumber somewhere in my code to let it know what field to go to? Anyone
Last edited by pea33nut; Jun 28th, 2004 at 09:27 AM.
-
Jun 28th, 2004, 09:41 AM
#16
Thread Starter
Lively Member
Last edited by pea33nut; Jun 28th, 2004 at 12:17 PM.
-
Jun 28th, 2004, 12:49 PM
#17
Thread Starter
Lively Member
I have stumped the forum.
-
Jun 28th, 2004, 01:04 PM
#18
Frenzied Member
Ok, your Insert is messed up. It should be:
INSERT INTO AUTO (FieldName) VALUES ('" & Text & "'")
You aren't telling it what field to fill with your value....
Sean
Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|