473,903 Members | 3,835 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to connect/save/update data to the database ?

2 New Member
Hi there,

I just create a form that contains name, Address, City, State, Zip, Question Checked box, and dropdown list selection for the user to fill-out the answer in the texbox, checkbox or choose the selection the option...

For example here is the Form properties I setup:

Text="Name" Text ID="LabelName" TextBox="TextBo xName"
Text="Address" Text ID="LabelAddres s" TextBox="TextBo xAddress"
Text="City" Text ID="LabelCity" TextBox="TextBo xCity"
Text="State" Text ID="LabelState " TextBox="TextBo xState"
Text="Zip" Text ID="LabelZip" TextBox="TextBo xZip"
Text="QuestionA Text" Text ID="LabelQuesti onAText" CheckBox ="TextBoxQuesAY es"
Text="QuestionA Text" Text ID="LabelQuesti onAText" CheckBox ="TextBoxQuesAN o"
Text="Favorite Color" Text ID="LabelFavori teColor" List List=" red, white, blue"

I also create the access database with field names corresponding to the form above

Information.mdb <----Access database named Information, Here are the fieldnames
ID ------> type autonum
Name ----->type text
Address ----->type text
City ----->type text
State ----->type text
Zip ----->type text
Name ----->type text
QuestionAYes ------------->type checkbox
QuestionANo ------------->type checkbox
Favorite_Color---------->type text

I can run it to the web on the local host and type in the information in it, but when I click "Submit Button" I want it to update those information and save to the access database when click Submit button. I use OleDbconnection to connect it to the access database and it shows that the link like this Provider=Micros oft.Jet.OLEDB.4 .0;Data Source="C:\Docu ments and Settings\BOS\My Documents\Visua l Studio 2005\Projects\P ersonalInformat ion\Information .mdb"

So how do I set up the Olecommand, databind, update, and insert those information into the access database? Can someone show me the code in ASP.NET IN C# Please ? I'm very new to all this :-) thanks in advance
May 31 '07 #1
2 4471
2 New Member
Here is my code, I wrote it in ASP.NET IN C# , but it's not connecting to the access database eventhough I create the field names corresponding to the values of the textbox. Maybe someone can show an error or a better way to connect /save/update the data, thanks in advance

using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Data.Ole Db;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.Configur ation;

namespace FORM
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.P age
protected System.Web.UI.W ebControls.Butt on ButtonSubmit;
protected System.Web.UI.W ebControls.Butt on ButtonClear;
protected System.Web.UI.W ebControls.Text Box TextBoxName;
protected System.Web.UI.W ebControls.Text Box TextBoxAddress;
protected System.Web.UI.W ebControls.Text Box TextBoxCity;
protected System.Web.UI.W ebControls.Text Box TextBoxState;
protected System.Web.UI.W ebControls.Text Box TextBoxZip;
protected System.Web.UI.W ebControls.Chec kBox No;
protected System.Web.UI.W ebControls.Chec kBox Yes;
private ArrayList nameList;

private void Page_Load(objec t sender, System.EventArg s e)
/*if (!Page.IsPostBa ck)

// Put user code to initialize the page here

#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
InitializeCompo nent();

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
this.ButtonClea r.Click += new System.EventHan dler(this.Butto nClear_Click);
this.ButtonSubm it.Click += new System.EventHan dler(this.Butto nSubmit_Click);
this.Load += new System.EventHan dler(this.Page_ Load);


private void getinformation ()
string myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbPath = ConfigurationSe ttings.AppSetti ngs.Get("FormDb ");
string retVal;
int i;
//nameList = new ArrayList();
//emailList = new ArrayList();
string mySelectQuery = "Select * from Information";
myConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" + dbPath;
myConnection = new OleDbConnection (myConnString);
myCommand = new OleDbCommand(my SelectQuery, myConnection);
myConnection.Op en();
myReader = myCommand.Execu teReader();
i = 0;
while (myReader.Read( ))
TextBoxName.Add (myReader["Name"] + "");
TextBoxAddress. Add( myReader["Address"] + "");
TextBoxCity.Add ( myReader["City"] + "");
TextBoxState.Ad d( myReader["State"] + "");
TextBoxZip.Add( myReader["Zip"] + "");
Yes.Add( myReader["CheckBoxYe s"] + "");
No.Add( myReader["CheckBoxNo "] + "");
retVal = i.ToString();
myReader.Close( );
myConnection.Cl ose();
catch (Exception e)
retVal = ("Error: " + e.Message + "dbPath = " + dbPath);
return retVal;


private string InsertInformati on(string myConnString)
//string myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbName = ConfigurationSe ttings.AppSetti ngs.Get("FormDb "); //declare in Web.config <add key="FormDb" value="C:\Inetp ub\wwwroot\FORM \Form.mdb"/>
string myUpdateQuery, mySelectQuery;
string retVal;
mySelectQuery = "Select Form, Name = " + "'" + iptName.Value.T rim() + "'";
myConnString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" + dbName;
myConnection = new OleDbConnection (myConnString);
myCommand = new OleDbCommand(my SelectQuery, myConnection);
myConnection.Op en();
myReader = myCommand.Execu teReader();
if (myReader.Read( ))
myUpdateQuery = "Update Form Set Name='" + TextBoxName.Val ue.Trim() + "' Where Form= '" + TextBoxName.Val ue.Trim() + "'";
myUpdateQuery = "Update Form Set Address='" + TextBoxAddress. Value.Trim() + "' Where Form= '" + TextBoxAddress. Value.Trim() + "'";
myUpdateQuery = "INSERT INTO Form (Name, Address, City, State, Zip, CheckboxYes, CheckboxNo ) Values (";
myUpdateQuery = myUpdateQuery + "'" + TextBoxName.Val ue.Trim() + "',";
myUpdateQuery = myUpdateQuery + "'" + TextBoxAddress. Value.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxCity.Val ue.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxState.Va lue.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxZip.Valu e.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + Yes.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + No.Trim() + "')";
myReader.Close( );
if (myConnection.S tate != ConnectionState .Open)
myConnection.Op en();
OleDbCommand newCommand = new OleDbCommand(my UpdateQuery, myConnection);
newCommand.Exec uteNonQuery();
myConnection.Cl ose();
retVal = "Success: InsertRequestor Infor";
catch (Exception e)
retVal = ("Error: " + e.Message + "dbPath = " + dbName);
return retVal;

private void ButtonClear_Cli ck(object sender, System.EventArg s e)
TextBoxName.Tex t = " ";
TextBoxAddress. Text = " ";
TextBoxCity.Tex t = " ";
TextBoxState.Te xt = " ";
TextBoxZip.Text = " ";
Yes.Text = " ";
No.Text = " ";


private void ButtonSubmit_Cl ick(object sender, System.EventArg s e)
string retVal;

if (TextBoxName.te xt == null) || (TextBoxAddress .text == null)|| (TextBoxCity.te xt == null)||
(TextBoxState.t ext == null)|| (TextBoxZip.tex t == null)|| (Yes.text == null)||(No.text == null)
retVal = " Please fill in all the information";
getinformation ();
InsertInformati on ();

May 31 '07 #2
Kara Hewett
27 New Member
The connection to the database should use OLEDB.

Expand|Select|Wrap|Line Numbers
  1. Sub DB_Utility()
  2.   Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
  3.     strConnection += "Data Source=Northwind.mdb"
  4.     data_src.text = strConnection
  5.   Dim strSQL as string = "SELECT FirstName, LastName FROM Employees"
  6.   Dim strResultsHolder as string
  8.   Dim objConnection as New OledbConnection(strConnection)
  9.   Dim objCommand as New OledbCommand(strSQL, objConnection)
  10.   Dim objDataReader as OledbDataReader
  12.   try
  13.     objConnection.Open()
  14.     con_open.text="Connection opened successfully.<br />"
  15.   objDataReader = objCommand.ExecuteREader()
  17.   Do While objDataReader.Read()=True
  18.     strResultsHolder +=objDataREader("FirstName")
  19.     strResultsHolder +="&nbsp;"
  20.     strResultsHolder +=objDataREader("LastName")
  21.     strResultsHolder +="<br>"
  22.   Loop
  24.   objDataReader.Close()
  25.     objConnection.Close()
  26.     con_close.text="Connection closed.<br>"
  27.   divListEmployees.innerHTML = strResultsHolder
  28.   catch e as Exception
  29.     con_open.text="Connection failed to open successfully.<br>"
  30.     con_close.text=e.ToString()
  31.   end try
  32. end Sub
Apr 30 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

by: Andras Gilicz | last post by:
Hi VB fans I'm working on a relatively large project in VB6 with about a dozen forms, including graphs, labels, text boxes, etc. The software itself is actually a flow simulator with more or less complex technical calculations, several input variables. I would like to equipp the starting panel with the usual New, Open, Save, Save As, Close etc. menus (like in Excel, or Word, etc.) What is the best way to accomplish Save, or Save As?...
by: Clay Beatty | last post by:
When you create database diagrams in Enterprise Manager, the details for constructing those diagrams is saved into the dtproperties table. This table includes an image field which contains most of the relevant infomation, in a binary format. SQL Enterprise manager offers no way to script out those diagrams, so I have created two Transact SQL components, one User Function and one User Procedure, which together provide a means to script...
by: banz | last post by:
Hello I have a problem to resolve: I wrote a Perlscript which caches data from a server (local on my machine) I would like to have a other connection to a remote server but I don't know how to define the servername / hostname in my Perl Progrem.. Here is the code:
by: Alpha | last post by:
Hi, I have a window based program. One of the form has several textboxes and a datagrid. The textboxes are bind to the same dataset table as the datagrid and the text changes to reflect different row selected in the datagrid. I want to save the changes that user make in the textboxes when they select a different row in the datagrid. I tried capturing the textbox.text at datagrid's CurrentCellChanged event but by then the textbox.text...
by: Daniel Bass | last post by:
I setup a asp.net project running on http://localhost/ which connects to a database on another server running sqlserver... I was able to connect to the database and create my application no problem. The sqlserver database server had windows 2000 without any SP's or updates. This was no problem and even though the OS hadn't the .net framework installed, everything worked fine. the requirements for that sqlserver database server have...
by: vicky | last post by:
Hello Experts, Trying to run sample Postgrel's ECPG(Embedded SQL)programs on RHL 9.0. Im unable to connect to PostgreSQL database (sirishadb) when I run the program .... # su postgres (enter)
by: ankz | last post by:
Hi Guys I have got 3 tables - in Access Database with Student ID as primary Key in all and all tables are linked with 1 to 1 relationship. I have code the everying with manual program to access information of tables on Windows Form. It is working properly. i can see all the records properly.
by: BD | last post by:
I am developing C# win form app to work with remote database on SQL Server 2005. Problem scenario is as follows: 1. a form is open that has downloaded dataset to local cache 2. computer is put into stand-by or hibernation 3. later, computer is brought out of stand-by or hibernation 4. when trying to save or close form, SQL exception comes up (remote system closed conn) I know this relates to connection pooling, but I am having a...
by: TonyJ | last post by:
Hello! I'm using VS2005. I'm looking at ADO.NET and have found some test tutorial solution on microsoft MSDN. The one that I'm looking at now is called Walkthrough: Saving Data to a Database (Single Table) I have followed the walkthrought exectly but it doesn't work as expected.
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.