|
-
Apr 11th, 2003, 08:16 AM
#1
Thread Starter
Junior Member
insert records into database
Hi,
I have been running around in circles trying to figure out why this won't work and have finally resorted to seeking advice.
I have a datagrid that is used to display my QueryCode table (SQL server). I am getting the Table values from a dataset created by my data adapter. I have a button and two textboxes (for entering the values). When I click on the button I just want to be able to pass an sql statement with the values from the textboxes, to an sqlcommand who will add the record to the database, but it just doesn't seem to want to process it!
What am I doing wrong...!
Here's the code:
Code:
Private Sub AddButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddButton.Click
Dim query_code As String
Dim query_ref As String
Dim sql_add As String
query_code = txtQueryCode.Text
query_ref = txtQueryRef.Text
sql_add = "INSERT INTO QueryCode(QueryCode, QueryRef) VALUES (" + query_code + "," + query_ref + ")"""
'SqlConnection1 is the connection created with the DataAdapter
SqlCommand1.Connection = SqlConnection1
SqlCommand1.CommandType = CommandType.Text
'Setting the command text to the SQL statement declared above
SqlCommand1.CommandText = sql_add
SqlConnection1.Open()
Try
SqlCommand1.ExecuteNonQuery()
Catch ex As Exception
Label1.Text = ex.ToString
End Try
SqlConnection1.Close()
End Sub
-
Apr 11th, 2003, 01:48 PM
#2
New Member
Does the exception fire? Or doesn't do anything at all?
-
Apr 11th, 2003, 01:54 PM
#3
Addicted Member
What is the data type of the fields QueryCode and QueryRef (in your db)? If those are string/character data types, then you need a single quote around your values in your statement.
Also, it looks like you can remove that last 2 "s from your "sql_add =" statement.
-
Apr 14th, 2003, 05:35 AM
#4
Thread Starter
Junior Member
Thanks, I had forgotten that my query_ref was characters so have put in the single quotes into the sql statement and it seems to be adding and displaying the new data fine.
There is a slight glitch in that when I am on the page of the datagrid where the new record should be slotted in it doesn't seem to refresh it until I leave that page and come back...?
I just have one more wee query, I've got a delete command in my datagrid and am trying to establish which record has been selected, and what it's values are. I don't wish to use parameters. I have a simple statement as such:
Code:
Dim key As String
key = dg.DataKeys(e.Item.ItemIndex).ToString()
When I hover over the ItemIndex at runtime it is 2. But It keeps giving me this error:
Code:
System.ArgumentOutOfRangeException: Index was out of range.
Must be non-negative and less than the size of the collection. Parameter name: index
Again, cheers for your help.
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
|