SQL/ASP Insert/Update data

507 Views Asked by At

I have a SQL database table with User info - ID, Name, Address, City, State, Zip, Phone, Email. I display this data for the logged-in user on a web page - so only one user data will be displayed at a time. User will be able to INSERT & UPDATE data.

I initially coded the page with Textboxes & used a dataset-SQLDataAdapter to communicate with SQL.

I spent a lot of time trying to get the Textboxes lined up etc. Although, the code is working, I am not sure if this this is the most efficient way.

May be use a data control? What is the best way to display & update the data ?

1

There are 1 best solutions below

4
Abe Miessler On BEST ANSWER

I think that SQLDataAdapater is geared towards working with multiple rows of data. if you are only working with one row at a time, then I would probably stick with SQLCommand and create a parameterized query from your textBox values. Example:

string queryString = 
    "INSERT INTO UserInfo Name, Address, City VALUES (@Name, @Address, @City)";
using (SqlConnection connection = new SqlConnection(
           connectionString))
{
    SqlCommand command = new SqlCommand(
        queryString, connection);
    command.Parameters.Add(new SqlParameter("@Name", tb_Name.Text));
    //add other params too
    connection.Open();
    command.ExecuteNonQuery();
}