C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Update the Datatable to SQLTable


Posted Date: 14 Mar 2008    Resource Type: Articles    Category: Web Applications

Posted By: Danasegarane.A       Member Level: Diamond
Rating:     Points: 10



This article will explain the method of updating the Datatable to SQLTale



I am using the VS2005 and SQL as the BackEnd



Code for Updating the underlaying datatable



SqlCommand sqlcmd = new SqlCommand("Select * from customers"); //Using the northwhind
//SqlConnection is class which will open the connection to Northwind Database
sqlcmd.Connection = SQLConnections.sqlCon;
SqlDataAdapter sqda = new SqlDataAdapter(sqlcmd);
DataTable dtTemp = new DataTable();
SqlCommandBuilder sqlbuilder = new SqlCommandBuilder(sqda);
SQLConnections.OpenConnection();
sqda.Fill(dtTemp); //fill the values returned into temp data table
DataRow dr = dtTemp.NewRow(); //Creating a new row
dr[”CustomerID”] = “100"; //Add the custumer id //Required
dr[”CompanyName”] = “SolidSollutions”; //Adding the companyName
dr[”ContactName”] = “Danasegarane”;
dr[”ContactTitle”] = “Programmer”;
dr[”Address”] = “India”;
dr[”City”] = “India”;
dr[”Region”] = “India”;
//I have left other columns fields because they are not the mandatory one
dtTemp.Rows.Add(dr); //Adding the row to the datatable
sqda.Update(dtTemp); //Update the related dataadapeter
sqda.Dispose(); //dispose
SQLConnections.CloseConnection();




The SQLConnection Class which is used in the previous code




public class SQLConnections
{

public static SqlConnection sqlCon;
public static string strErrorDescription = string.Empty;
public static void OpenConnection()
{
string SQLServerName = string.Empty;
string SQLDataBase = string.Empty;
string SQLConnectionString = string.Empty;
SQLServerName = ConfigurationManager.AppSettings[”SQLServerName”];
SQLDataBase = ConfigurationManager.AppSettings[”SQLDataBase”];
SQLConnectionString += “data source=” + SQLServerName + “;”;
SQLConnectionString += “initial catalog=” + SQLDataBase + “;”;
SQLConnectionString += “user id=sa;Password=test;”;
SQLConnectionString += “persist security info=True “;
sqlCon = new SqlConnection(SQLConnectionString);
try
{
if (sqlCon.State == ConnectionState.Closed)
{
sqlCon.Open();
}
}
catch (Exception ex)
{
strErrorDescription = ex.Message;
}
}
//Change the SQLServerName and SQLDataBase to user requirements


You can extend this code to update the datagrid to SQLTable
For More Information Please Refer : http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx







Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Enable/Disable RequiredFieldValidator with Javascript
Previous Resource: How to display user’s full name using Active directory?
Return to Discussion Resource Index
Post New Resource
Category: Web Applications


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing services

Contact Us    Privacy Policy    Terms Of Use