ASP.net 2 daft question

Baz

Baz

Soldato
Joined
9 Dec 2002
Posts
4,376
Location
Peterborough
This may seem like a schoolboy question about ASP.net 2.0 but anyway!

I have been using visual studio 2005 for a few years now creating reports and applications that just send variables to the SQL server and return data which I display, this is great, works a treat.

Now I want to enter data into SQL tables, and I am stuck as to how to do it. In classic asp you created variables which I passed to a stored procedure which inserted them into the tables.

In VS so far I have created a SQL data source linked to the table I want to populate, but I don't want to display anything from it, just have text boxes/drop downs etc and something like a submit button...

So, how is it done in VS2005?
 
Dont do it via VS.
Code:
SqlConnection conNorthwind;
	string  strInsert;
	SqlCommand cmdInsert;

	conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Northwind" );
	strInsert = "Insert Products ( ProductName, UnitPrice ) Values (@ProductName, @UnitPrice )";
	cmdInsert = new SqlCommand( strInsert, conNorthwind );
	cmdInsert.Parameters.Add( "@ProductName", txtProductName.Text );
	cmdInsert.Parameters.Add( "@UnitPrice", SqlDbType.Money ).Value = txtUnitPrice.Text;
	conNorthwind.Open();
	cmdInsert.ExecuteNonQuery();
	conNorthwind.Close();
Code:
SqlConnection conNorthwind;
	string  strInsert;
	SqlCommand cmdInsert;

	conNorthwind = new SqlConnection( @"Server=localhost;Integrated Security=SSPI;database=Northwind" );
	cmdInsert = new SqlCommand( "InsertProducts", conNorthwind );
	cmdInsert.CommandType = CommandType.StoredProcedure;
	cmdInsert.Parameters.Add( "@ProductName", "Milk" );
	cmdInsert.Parameters.Add( "@UnitPrice", 12.45 );
	conNorthwind.Open();
	cmdInsert.ExecuteNonQuery();
	conNorthwind.Close();
That what you are after?
 
its nearly the same really, you create your sqlconnection objecta nd feed it your connection string. Then create your SqlCommand object and give it the name of your stored procedure then set the command objects commandtype to StoredProcedure. Then add all the parameters you want to send to the stored procedure.

Think you can also do it using a dataset which will allow you to knock something up with a little less code but I prefer the code option personally.
 
Not really looking to do it fully with code if you get my meaning!

I am trying to do as much as I can through the VS2005 GUI which seems to do what I want but nothing gets inserted. I have a SQL data connection linked to a form view, and in the formview INSERTITEM Template I have three text boxes bound to the datasource, and a Linkbutton with INSERT as its command, but only NULL gets passed through..
 
Back
Top Bottom