I have created a form for customer registration in Visual Web Developer E.E. (http://kunde.xtrava.no/databasetesting/insertcustomer.aspx)
but I don't know the C#-code to get data from the form to the Access database(mdb) when the user clicks the Store Customer.
In your submit button, you have to hook onto an "OnClick" event (in visual studio, double click the button, and it'll create it for you), in there, you can get all the values from your text field and then insert the data into your database using whatever
data access method you prefer.
Thanks for your advice. I am getting there now. My way of learning is looking at examples. Now I have finally found some examples that does both MS Access and C#.
leb_bambus
Member
18 Points
62 Posts
Insert data from form to mdb-database. (c#)
Sep 13, 2007 01:40 PM|LINK
Hi!
I have created a form for customer registration in Visual Web Developer E.E. (http://kunde.xtrava.no/databasetesting/insertcustomer.aspx)
but I don't know the C#-code to get data from the form to the Access database(mdb) when the user clicks the Store Customer.
this is insertcustomer.aspx:<%
@ Page Language="C#" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table>
<tr><td>Name:</td><td><asp:TextBox ID="Navn" runat="server"></asp:TextBox></td></tr>
<tr><td>Addresse:</td><td><asp:TextBox ID="Adresse" runat="server"></asp:TextBox></td> </tr>
<tr><td>Postcode:</td><td><asp:TextBox ID="Postnr" runat="server"></asp:TextBox></td> </tr>
<tr><td>City:</td><td><asp:TextBox ID="Poststed" runat="server"></asp:TextBox></td></tr>
<tr><td>Email:</td><td><asp:TextBox ID="Epost" runat="server"></asp:TextBox></td></tr>
<tr><td>Phone no:</td><td><asp:TextBox ID="Tlf" runat="server"></asp:TextBox></td></tr>
<tr><td colspan="2"><asp:Button ID="Submit" runat="server" Text="Store customer" /></td></tr>
</table>
</div>
</form>
</body>
</html>
I have a page with a gridview showing registered "customers": (http://kunde.xtrava.no/databasetesting/default.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="BestillerID" DataSourceID="AccessDataSource1">
<Columns>
<asp:BoundField DataField="BestillerID" HeaderText="BestillerID" InsertVisible="False" ReadOnly="True" SortExpression="BestillerID" />
<asp:BoundField DataField="Navn" HeaderText="Navn" SortExpression="Navn" />
<asp:BoundField DataField="Adresse" HeaderText="Adresse" SortExpression="Adresse" />
<asp:BoundField DataField="Postnr" HeaderText="Postnr" SortExpression="Postnr" />
<asp:BoundField DataField="Poststed" HeaderText="Poststed" SortExpression="Poststed" />
<asp:BoundField DataField="Tlf" HeaderText="Tlf" SortExpression="Tlf" />
<asp:BoundField DataField="Epost" HeaderText="Epost" SortExpression="Epost" />
<asp:BoundField DataField="IP" HeaderText="IP" SortExpression="IP" />
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sommerrevy07_ny.mdb" SelectCommand="SELECT * FROM [tblBestiller]"></asp:AccessDataSource>
<br />
<a href="insertcustomer.aspx">insert new customer</a></div>
</form>
</body>
</html>
Liming
Contributor
5367 Points
1076 Posts
Re: Insert data from form to mdb-database. (c#)
Sep 13, 2007 02:40 PM|LINK
hi leb,
In your submit button, you have to hook onto an "OnClick" event (in visual studio, double click the button, and it'll create it for you), in there, you can get all the values from your text field and then insert the data into your database using whatever data access method you prefer.
I suggest you take a look at the tutorials here http://www.asp.net/learn/data-access/
Jumptree ASP.NET 2.0 Project Management, Bug Tracking & Task Management - Encourages collaboration, provides transparency and comes with metrics.
JQuery Gantt, JQuery Calendar, SiteMinder/AD Authentication, Customizable Languages, Custom Fields, Lucene.Net Search, and more
naturehermit
Star
14610 Points
3046 Posts
Re: Insert data from form to mdb-database. (c#)
Sep 13, 2007 03:04 PM|LINK
You can do it two ways, one is either to have a details view with all your form elements as demonstrated here...
http://www.propstm.net/2006/02/01/inserting-records-into-and-access-database-using-aspnet-20-%E2%80%93-tutorial/
The other is to hard code it in the form like so create a button handler and put similar code in...
//Get the connection string from web.configSqlConnection
connection = new SqlConnection(NAMEOFCONECTIONSTRING); //GET the command string (check your tablestring
cmdString = ("Insert into YOUR TABLE values @Name,@Address, @City, @PostCode, @PNo,@Email)"); SqlCommand cmd = new SqlCommand(cmdString, connection);Add parameters
cmd.Parameters.Add(new SqlParameter("@Name", )); (DONT kNOW YOUR TABLE STRUCTUREcmd.Parameters[
"@Name"].Value = Server.HtmlEncode(NaVN.Text.Trim()); //Your textbox
// REST AS AN EXAMPLEcmd.Parameters.Add(
new SqlParameter("@EstName", SqlDbType.VarChar, 150)); cmd.Parameters["@EstName"].Value = Server.HtmlEncode(txtEstablishmentName.Text);cmd.Parameters.Add(
new SqlParameter("@Address1", SqlDbType.VarChar, 200)); cmd.Parameters["@Address1"].Value = Server.HtmlEncode(txtInstAdd.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@Address2", SqlDbType.VarChar, 200)); cmd.Parameters["@Address2"].Value = Server.HtmlEncode(txtInstAdd2.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@Country", SqlDbType.VarChar)); cmd.Parameters["@Country"].Value = Server.HtmlEncode(txtInstRegion.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@TownCity", SqlDbType.VarChar)); cmd.Parameters["@TownCity"].Value = Server.HtmlEncode(txtInstCity.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@PC", SqlDbType.VarChar)); cmd.Parameters["@PC"].Value = Server.HtmlEncode(txtInstPC.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@PNo", SqlDbType.BigInt)); cmd.Parameters["@PNo"].Value = Server.HtmlEncode(txtInstPNo.Text.Trim());cmd.Parameters.Add(
new SqlParameter("@EntType", SqlDbType.VarChar, 150)); cmd.Parameters["@EntType"].Value = enType;cmd.Parameters.Add(
new SqlParameter("@EstType", SqlDbType.VarChar, 100)); cmd.Parameters["@EstType"].Value = estType;cmd.Parameters.Add(new SqlParameter("@userId", userId));connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
}
Response.Write(ex.Message +
"Additional Details were not added, Please use edit page to edit details");If you mean insert/Update in your gridVIEW then you need to add, u need update and insert command
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/App_Data/sommerrevy07_ny.mdb" SelectCommand="SELECT * FROM [tblBestiller]"
UpdateCommand ="Update....(dont know your table stuff)
InsertCommand="Insert into (dont know your table stuff
></asp:AccessDataSource>
General syntax
updatecommand="Update Employees SET FirstName=?,LastName=?,Title=? WHERE EmployeeID=@EmployeeID" //Where EMployees is the table name
InsertCommand="INSERT INTO Employees(LastName, FirstName) VALUES (@LastName, @FirstName);
SELECT @EmpID = SCOPE_IDENTITY()"
leb_bambus
Member
18 Points
62 Posts
Re: Insert data from form to mdb-database. (c#)
Sep 20, 2007 10:48 AM|LINK
Thanks for your advice. I am getting there now. My way of learning is looking at examples. Now I have finally found some examples that does both MS Access and C#.
-leb
leb_bambus
Member
18 Points
62 Posts
Re: Insert data from form to mdb-database. (c#)
Sep 20, 2007 10:51 AM|LINK
Thanks for your advice. The Visual Web Developer E.E. tutorial was great. I am checking out the hard-code-example soon. Thank you once again.
-leb
mukundbtech
Member
12 Points
1 Post
Re: Insert data from form to mdb-database. (c#)
Nov 14, 2007 06:17 AM|LINK
hi,
This is the c# code to insert a text box value to an access db..
use
using System.Data.OleDb; protected void Button1_Click(object sender, EventArgs e){
OleDbConnection a = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\rao.mdb"); string dbcommand = "INSERT into rao (Name) " + " VALUES ('" + TextBox2.Text + "')"; OleDbDataAdapter b = new OleDbDataAdapter(dbcommand, a);DataSet dset = new DataSet();b.Fill(dset);
}
mukund
SPi Tech
mukesha2201
Member
2 Points
1 Post
Re: Insert data from form to mdb-database. (c#)
Aug 26, 2009 12:41 AM|LINK
Hi I have following block of code for Submit the form in sql server db.
-------------------------------------
private void b_VC_Submit_Click(object sender, EventArgs e){
//Get the connection string from web.config
SqlConnection connection = new SqlConnection("Data Source=LYUDAA-P390;Initial Catalog=MI;Integrated Security=True");
connection.Open();
//GET the command string (check your table
string cmdString = ("Insert into tbl_VoucherCash values @P_VC_VoucherNo, @VC_AcountHead, @VC_Amount, @VC_AccountBalance, @VC_Narration, @VC_TransactionType, @VC_Notes)"); SqlCommand cmd = new SqlCommand (cmdString, connection);cmd.Parameters[
"@P_VC_VoucherNo"].Value = (tb_VC_VoucherNo.Text.Trim());cmd.Parameters[
"@P_VC_VoucherNo"].Value = (tb_VC_VoucherNo.Text.Trim());cmd.Parameters[
"@VC_AcountHead"].Value = tb_VC_VoucherNo.Text.Trim();cmd.Parameters[
"@VC_Amount"].Value = tb_VC_VoucherNo.Text.Trim();cmd.Parameters[
"@VC_AccountBalance"].Value = tb_VC_VoucherNo.Text.Trim();cmd.Parameters[
"@VC_Narration"].Value = tb_VC_VoucherNo.Text.Trim();cmd.Parameters[
"@VC_TransactionType"].Value = tb_VC_VoucherNo.Text.Trim();cmd.Parameters[
"@VC_Notes"].Value = tb_VC_VoucherNo.Text.Trim();cmd.ExecuteNonQuery();
connection.Close();
}
------------------------------------------------
this gives following error when I submit the from with submit button.
IndexOutofRangeExcetion was unhandled.
An SqlParameter with ParameterName '@P_VC_VoucherNo' is not contained by this SqlParameterCollection.