Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to update sql server table from ASP.NET. What is the easiest way to do it?
Posted

Hi Saksham
You should consider familiarizing yourself with entity framework, but if you want to achieve just an update fast and easy:

C#
using System.Data.SqlClient; 

protected void Page_Load(object sender, EventArgs e)
        {
            string connectionString = "blablawhichconnectstomyddatabase";
            string sql = "update tbl_x set MyColumn = '{0}' where MyId = {1}";
            int idKey = 1;
            sql = string.Format(sql, "myvalue", idKey);

            using (var cn = new SqlConnection(connectionString))
            {
                var cmd = new SqlCommand(sql, cn);
                cmd.ExecuteNonQuery();
            }
}


see tbl_x substitute with your table name and MyColumn with your column and MyId with your primary table ID value.

There are many more fancy ways of getting the task done, but just a simple update, note the quoting around string and the non quoting when simple data type like the MyId.

Alternatively you can have a crack at using object data sources, they're very good if you like guides and wysiwyg's http://msdn.microsoft.com/en-us/library/aa581783.aspx[^]
 
Share this answer
 
v3
Comments
Saksham_Agrawal 5-Mar-14 5:48am    
Thank you for the reply. But I was not able to connect SQL Server with HTML Table so I was displaying the data with GridView. Can you help me how to connect SQL with HTML Table
Thomas Nielsen - getCore 5-Mar-14 5:56am    
html table is a server side representation of a tabular viewing structure in html.
It has nothing to do with a data table, so it cannot be done.
Maybe from what you write you will see the answer to what you need in object data sources, will update solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900