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



My Profile

Gifts

Active Members
TodayLast 7 Days more...







Simple program to add/delete data to database


Posted Date: 08 May 2008    Resource Type: Code Snippets    Category: ADO.NET

Posted By: komaladevi       Member Level: Gold
Rating:     Points: 10




using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, System.EventArgs e)
{
string DBpath = Server.MapPath("app_data/addressBook.mdb");
string ConnStr;
string SQL;

ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + DBpath + ";";
OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr);
MyOleDbConn.Open();

try
{
SQL = "select * from tblAddressBook";
OleDbCommand myOleDbCommand = new OleDbCommand(SQL, MyOleDbConn);
object Result;
Result = myOleDbCommand.ExecuteScalar();
Value.Text = "There are " + Result.ToString() + " products in the database";

OleDbDataAdapter myOleDbAdapter = new OleDbDataAdapter();
myOleDbAdapter.SelectCommand = new OleDbCommand(SQL, MyOleDbConn);
DataSet myDS = new DataSet();
myOleDbAdapter.Fill(myDS);

GridView1.DataSource = myDS;
GridView1.DataBind();

OleDbDataReader myOLEDataReader = myOleDbCommand.ExecuteReader();

Response.Write("");
while(myOLEDataReader.Read())
{
Response.Write(myOLEDataReader.GetString(1) + " " + myOLEDataReader.GetString(2) + " " + myOLEDataReader.GetString(3) + "
");
}
Response.Write("
");
}
catch
{

}
finally
{
MyOleDbConn.Close();
}
}
protected void Button2_Click(object sender, System.EventArgs e)
{
string DBpath = Server.MapPath("app_data/addressBook.mdb");
string ConnStr;

ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + DBpath + ";";

OleDbConnection myOleDbConnection = new OleDbConnection(ConnStr);
string insertSQL;
insertSQL = "INSERT into tblAddressBook (name,phone,email,address) VALUES ('" + txtName.Text + "','" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtAddress.Text + "');";
OleDbCommand myOleDbCommand = new OleDbCommand(insertSQL, myOleDbConnection);
myOleDbCommand.Connection.Open();
myOleDbCommand.ExecuteNonQuery();
myOleDbCommand.Connection.Close();
}
}




Responses

Author: sandeep    29 May 2008Member Level: Bronze   Points : 2
can u tell me how to delete a row


Author: sandeep    29 May 2008Member Level: Bronze   Points : 2
can u tell me how to delete a row


Author: Bindu    07 Jun 2008Member Level: Silver   Points : 2
It is simple. Instead of insertSql define string called DeleteSql.

string DeleteSql;
DeleteSq;= "DELTE FROM tblAddressBook WHERE
NAME = '" + txtName.Text "' "

OleDbCommand myOleDbCommand = new OleDbCommand(DeleteSq;, myOleDbConnection);
myOleDbCommand.Connection.Open(); myOleDbCommand.ExecuteNonQuery(); myOleDbCommand.Connection.Close();

Nice code snippet Komala



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: how to use a SqlDataAdapter to get data from a database into a DataSet.
Previous Resource: Get Number of rows affected by SqlDataSource / ObjectDataSource select Query
Return to Discussion Resource Index
Post New Resource
Category: ADO.NET


Post resources and earn money!
 
Related Resources



Read TATA Nano reviews.
dotNet Slackers   BizTalk Adaptors    Web Design   Scripts
Are you waiting for engineering entrance result? Watch here for engineering entrance results.
silicone halloween masks

Contact Us    Privacy Policy    Terms Of Use