Hi guys, I will try and explain this as clearly as I can.
I have setup my SQL statements for INSERT in Visual Studio 2005, and I am using C#.Net as my language.
I can use the built in tools fine to display data and add new records to it. However I want to be able to create my own layout using text boxes and then use a submit button to call the INSERT statement and insert what is included in the text boxes into my database.
I assume I need to name my text boxes the same as they are in the INSERT statement. As an example my insert statement would look something like this:
INSERT INTO [tblUserDetail] (id, name, age) VALUES (@txtid, @txtname, @txtage)
and the name of the 3 text fields would be (txtid, txtname, and txtage)
Now that is setup what code do I need to put in the button to call that INSERT statement that is in my data source?
If you can offer any guidance at all that would be great. I had a search around but could only find people who were having problems with the built in dataview grid etc which I have got working fine.
Loading
Bechir BejaouiPosted Mar 21, 2008, 8:47 AM
You can use Connection, Command and DataReader objects to achieve you goal
I give you this code written in OleDbContext
OleDbConnection oConnection = new OleDbConnection("..."); /* Put your connection string instead of ...*/
string param1 = txtid.Text;
string param2 = txtname.Text;
string param3 = txtage.Text;
string Query = "INSERT INTO [yourTable] ('id','name','age') VALUES (" + param1 + "," + param2 + "," + param3 + ")"
OleDbCommand oCommand = new OleDbCommand(Query , oConnection);
try
{
oConnection.Open();
OleDataReader oReader = oCommand.ExectueNonQuery();
}
catch(ApplicationException caugth)
Finally
{
oConnection.Close();
}