472,338 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

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

2
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="TextBoxName"
Text="Address" Text ID="LabelAddress" TextBox="TextBoxAddress"
Text="City" Text ID="LabelCity" TextBox="TextBoxCity"
Text="State" Text ID="LabelState" TextBox="TextBoxState"
Text="Zip" Text ID="LabelZip" TextBox="TextBoxZip"
Text="QuestionAText" Text ID="LabelQuestionAText" CheckBox ="TextBoxQuesAYes"
Text="QuestionAText" Text ID="LabelQuestionAText" CheckBox ="TextBoxQuesANo"
Text="Favorite Color" Text ID="LabelFavoriteColor" List List=" red, white, blue"
l

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=Microsoft.Jet.OLEDB.4.0;Data Source="C:\Documents and Settings\BOS\My Documents\Visual Studio 2005\Projects\PersonalInformation\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 4327
BOS
2
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.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;

namespace FORM
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button ButtonSubmit;
protected System.Web.UI.WebControls.Button ButtonClear;
protected System.Web.UI.WebControls.TextBox TextBoxName;
protected System.Web.UI.WebControls.TextBox TextBoxAddress;
protected System.Web.UI.WebControls.TextBox TextBoxCity;
protected System.Web.UI.WebControls.TextBox TextBoxState;
protected System.Web.UI.WebControls.TextBox TextBoxZip;
protected System.Web.UI.WebControls.CheckBox No;
protected System.Web.UI.WebControls.CheckBox Yes;
private ArrayList nameList;


private void Page_Load(object sender, System.EventArgs e)
{
/*if (!Page.IsPostBack)
{


// Put user code to initialize the page here
}*/
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.ButtonClear.Click += new System.EventHandler(this.ButtonClear_Click);
this.ButtonSubmit.Click += new System.EventHandler(this.ButtonSubmit_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion


private void getinformation ()
{
string myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbPath = ConfigurationSettings.AppSettings.Get("FormDb");
string retVal;
int i;
try
{
//nameList = new ArrayList();
//emailList = new ArrayList();
string mySelectQuery = "Select * from Information";
myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbPath;
myConnection = new OleDbConnection(myConnString);
myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
i = 0;
while (myReader.Read())
{
TextBoxName.Add (myReader["Name"] + "");
TextBoxAddress.Add( myReader["Address"] + "");
TextBoxCity.Add( myReader["City"] + "");
TextBoxState.Add( myReader["State"] + "");
TextBoxZip.Add( myReader["Zip"] + "");
Yes.Add( myReader["CheckBoxYes"] + "");
No.Add( myReader["CheckBoxNo"] + "");
i++;
}
retVal = i.ToString();
myReader.Close();
myConnection.Close();
}
catch (Exception e)
{
retVal = ("Error: " + e.Message + "dbPath = " + dbPath);
}
return retVal;
}


}

private string InsertInformation(string myConnString)
{
//string myConnString;
OleDbConnection myConnection;
OleDbCommand myCommand;
OleDbDataReader myReader;
string dbName = ConfigurationSettings.AppSettings.Get("FormDb"); //declare in Web.config <add key="FormDb" value="C:\Inetpub\wwwroot\FORM\Form.mdb"/>
string myUpdateQuery, mySelectQuery;
string retVal;
try
{
mySelectQuery = "Select Form, Name = " + "'" + iptName.Value.Trim() + "'";
myConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;
myConnection = new OleDbConnection(myConnString);
myCommand = new OleDbCommand(mySelectQuery, myConnection);
myConnection.Open();
myReader = myCommand.ExecuteReader();
if (myReader.Read())
{
myUpdateQuery = "Update Form Set Name='" + TextBoxName.Value.Trim() + "' Where Form= '" + TextBoxName.Value.Trim() + "'";
myUpdateQuery = "Update Form Set Address='" + TextBoxAddress.Value.Trim() + "' Where Form= '" + TextBoxAddress.Value.Trim() + "'";
}
else
{
myUpdateQuery = "INSERT INTO Form (Name, Address, City, State, Zip, CheckboxYes, CheckboxNo ) Values (";
myUpdateQuery = myUpdateQuery + "'" + TextBoxName.Value.Trim() + "',";
myUpdateQuery = myUpdateQuery + "'" + TextBoxAddress.Value.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxCity.Value.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxState.Value.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + TextBoxZip.Value.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + Yes.Trim() + "')";
myUpdateQuery = myUpdateQuery + "'" + No.Trim() + "')";
}
myReader.Close();
if (myConnection.State != ConnectionState.Open)
myConnection.Open();
OleDbCommand newCommand = new OleDbCommand(myUpdateQuery, myConnection);
newCommand.ExecuteNonQuery();
myConnection.Close();
retVal = "Success: InsertRequestorInfor";
}
catch (Exception e)
{
retVal = ("Error: " + e.Message + "dbPath = " + dbName);
}
return retVal;
}


private void ButtonClear_Click(object sender, System.EventArgs e)
{
TextBoxName.Text = " ";
TextBoxAddress.Text = " ";
TextBoxCity.Text = " ";
TextBoxState.Text = " ";
TextBoxZip.Text = " ";
Yes.Text = " ";
No.Text = " ";

}

private void ButtonSubmit_Click(object sender, System.EventArgs e)
{
string retVal;

if (TextBoxName.text == null) || (TextBoxAddress.text == null)|| (TextBoxCity.text == null)||
(TextBoxState.text == null)|| (TextBoxZip.text == null)|| (Yes.text == null)||(No.text == null)
{
retVal = " Please fill in all the information";
}
else
{
getinformation ();
InsertInformation ();
}



}
}
May 31 '07 #2
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
  7.  
  8.   Dim objConnection as New OledbConnection(strConnection)
  9.   Dim objCommand as New OledbCommand(strSQL, objConnection)
  10.   Dim objDataReader as OledbDataReader
  11.  
  12.   try
  13.     objConnection.Open()
  14.     con_open.text="Connection opened successfully.<br />"
  15.   objDataReader = objCommand.ExecuteREader()
  16.  
  17.   Do While objDataReader.Read()=True
  18.     strResultsHolder +=objDataREader("FirstName")
  19.     strResultsHolder +="&nbsp;"
  20.     strResultsHolder +=objDataREader("LastName")
  21.     strResultsHolder +="<br>"
  22.   Loop
  23.  
  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

4
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...
6
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...
4
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...
2
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...
5
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...
0
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) ...
1
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...
8
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...
1
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.