0

I have ALWAYS used the SQLDataSource to update my records in my SQL Server 2005 database, so i am not really sure how this would be done in normal code. I am using VB.NET but if you are a C# developer and want to show me some code that is more than welcome as well.

I am using ASP.NET 3.5...............

My user will see a page with 5 Text boxes with their contact details in. Those details I will get from a SQL Server DB and when they go and change the text in the text box and click submit, it must then save the changes.

This is very easy with the SQLDataSource but how can i code this myself?

Thanks in advance!

2 Answers 2

7

Something like the following in (vanilla) ADO.NET

using(var con = new SqlConnection("ConnectionString"))
{
    using (var cmd = con.CreateCommand())
    {
        // This Example uses a parameterized SQL statement
        // but you can also use SQL Server stored procedures

        cmd.CommandText = "UPDATE table SET field1 = @field1, field2 = @field2 WHERE id = @id"; // etc, etc    
        cmd.CommandType = CommandType.Text;

        // Add values for the parameter values for the 
        // parameter placeholders in the SQL statement
        cmd.Parameters.AddWithValue("field1", Textbox1.Text);
        cmd.Parameters.AddWithValue("field2", Textbox2.Text);  
        cmd.Parameters.AddWithValue("id", myObject.Id);                

        con.Open();

        // You might want to use ExecuteScalar instead and get a return value back
        // from the database to signal success
        cmd.ExecuteNonQuery();           
    }
}

A nice tutorial on ADO.NET - Accessing SQL Server data in C# with ADO.NET

Of course, using ASP.NET 3.5, you also have LINQ to SQL or the Entity Framework to choose from.

Then there is also NHibernate, a very popular open-source Object Relational mapper, which is excellent for basic CRUD applications.

4
  • I normally try and stay away from ADO.NET, any code for a btnSubmit button?
    – Etienne
    Jul 26, 2009 at 22:56
  • 2
    You try to stay away from ADO.NET why exactly? Somehow I get the feeling you are using ADO.NET and not realizing it. In any case, the code for a submit button would be structured exactly the same as the above code. It looks like you need some remedial help on data access, I think you'd do well to get a book on ADO.NET. Individual questions may not be the best way to learn this.
    – JohnFx
    Jul 26, 2009 at 22:59
  • I just like keeping my code out of HTML pages, if that maybe explains why i say i dont like ADO.NET?
    – Etienne
    Jul 26, 2009 at 23:03
  • This code would be best placed in a code-behind file and not intermixed with the aspx markup. Code Behind files/classes were a feature introduced in .NET 2.0 with the introduction of partial classes.
    – Russ Cam
    Jul 26, 2009 at 23:12
0

AI AI AI.........i was stupid.

I was suppose to place a IF NOT ISPOSTBACK statement on my PAGE LOAD event because i re-loaded my records from the DB when i clicked the Submit button and it updated the records with the old records!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.