Inserting data from textboxes to a database?

Lemon

Member
Joined
Jul 1, 2005
Messages
12
Programming Experience
1-3
Hey, got a little problem....again....hoping for some help!!

OK, I need to insert data into a SQL server database from textboxes, each textbox the entry for a different field, of course!

Its simple enough writing the queries, but how do you have query take the information in the textboxes as the data to be inserted?? I'm sure its relatively simple, I just dont know how.....
 
When you insert it this way, you can go like this:

either you can just supply values to the parameters like
VB.NET:
SQLDA.InsertCommand.Parameters(0).Value = TextBox1.text
SQLDA.InsertCommand.Parameters(1).Value = TextBox2.text
SQLDA.InsertCommand.Parameters(2).Value = TextBox3.text

or you can go like this:
VB.NET:
Dim strSQL As String = "INSERT INTO myTable (Name, Address, Phone) VALUES ('" & TextBox1.text & "', '" & TextBox2.text & "', '" & TextBox3.text & "')

Cheers ;)
 
Thanx alot, got that cleared up!! ;) Prefer the second method!!


Ummm, now more troubles!! How do I call the funtion I have the insert query in?? I currently have it set to the datasource of a datagrid....NOT working....
e.g. Grid1.DataSource = InsertData()
Grid1.DataBind()


Getting the following error message:
An invalid data source is being used for Grid1. A valid data source must implement either IListSource or IEnumerable.

So as I thought and insert query cant be called that way.... I AM kinda new to all this, so just how do you "call" a function so that it executes, but doesn't return anything....
A Sub just uses the CALL method, is there something similar to use with functions?
Or, there's prolly a simple way I just dont know about!
 
First of all, I highly recommend using the first method Kulrom suggested. It is a little more long-winded but it is less prone to error and it is more secure. I'd suggest getting into good habits early on.

Secondly, I've never tried it but I believe you can assign the result of a function as long as the return type implements one of those interfaces. If you had function that returned a DataTable, then it would be able to be assigned to the DataSource of the DataGrid. The function would have to execute a SELECT query though. You are inserting data into the database, not retrieving it from the database.

If you don't want your method to return a value, you can either declare it as a function but not specify a return type, or you can declare it as a procedure (Sub), which cannot have a return type.

If you are using a DataGrid, chances are you want to call Fill on an SqlDataAdapter to fill a DataTable or DataSet. You would assign the DataTable or DataSet to the DataSource property of the DataGrid, either before or after it is filled. You would then call Update on the SqlDataAdapter to commit any changes back to the database. You would have to create the necessary SqlCommand objects to perform any delete, insert or update operations required.

Edit:
Just a point to note: you cannot assign the result of a function to a variable if that function has no return type. A function without a return type is exactly the same as a procedure and is called in the same fashion.
 
THANX!! Its working properly now!! I'll prolly be back soon enough with more problems later on! But thank you for now anyway!
 
Back
Top