I am trying to store a new entry in my database trough the form created on the webpage. The database fields under which the user-entered values are stored are the following:
//Add each of the values from the textboxes
r["Cust_ID"] = lbxcontacts.SelectedItem.Value;
r["Contact_Name"] = name.Text;
r["Contact_Position"] = position.Text;
r["Contact_Telephone"] = telephone.Text;
r["Contact_Fax"] = fax.Text;
r["Contact_Email"] = email.Text;
The Cust_ID value is read from a listbox. It doesn't seem to like my syntax though and keepsgiving server errors.
I'm not sure what i should do to fix it, but i believe it has to do with the referencing i'm doing between the Cust_ID field and the listbox selection.
I am attaching the code below.
If anyone can help, you'll be a life saver!
Cheers.
Menelaos Prokos.
------------------------------------------------------------------
<%@ Page Language="C#" ContentType="text/html" %>
<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.OleDb" %>
<script language="c#" runat="server">
private String strConnection = "Provider=Microsoft.Jet.OleDb.4.0; data source=C:\\INetPub\\wwwroot\\Cube\\System.mdb;";
private String strSQL = "SELECT Cust_ID, Company FROM Companies";
private String ContactsTable = "Company_Contacts";
private OleDbConnection objConnection;
OleDbDataAdapter daCube;
DataSet dsCube;
private void Page_Load(object sender, EventArgs e)
{
objConnection = new OleDbConnection(strConnection);
String strSQL1 = "SELECT * FROM Company_Contacts";
daCube = new OleDbDataAdapter(strSQL1, objConnection);
OleDbCommandBuilder cb = new OleDbCommandBuilder(daCube);
OleDbCommand objCommand = new OleDbCommand(strSQL, objConnection);
objConnection.Open();
lbxcomps.DataSource = objCommand.ExecuteReader();
lbxcomps.DataTextField = "Company";
lbxcomps.DataValueField = "Cust_ID";
lbxcomps.DataBind();
//
objConnection.Close();
FillDataGrid();
}
private void FillDataGrid()
{
//Create dataset to contain records
dsCube = new DataSet("Company_Contacts");
//Fill the dataset
daCube.Fill(dsCube);
//Set the DataSource property of the DataGrid
newcontact.DataSource = dsCube.Tables[0].DefaultView;
//Bind the dataset data to the grid
newcontact.DataBind();
}
private void Store(object s, System.EventArgs e)
{
//If every box has been filled corectly (validation rules)
if(Page.IsValid)
{
//Create temp dataset for the new record
DataSet dsTemp = new DataSet();
//Fill the temp dataset
daCube.Fill(dsTemp);
//Create new row in the database
DataRow r = dsTemp.Tables[0].NewRow();
//Add each of the values from the textboxes
r["Cust_ID"] = lbxcontacts.SelectedItem.Value;
r["Contact_Name"] = name.Text;
r["Contact_Position"] = position.Text;
r["Contact_Telephone"] = telephone.Text;
r["Contact_Fax"] = fax.Text;
r["Contact_Email"] = email.Text;
//Add the new row in the dataset's rows collection
dsTemp.Tables[0].Rows.Add(r);
//Update the database with the temp dataset
daCube.Update(dsTemp);
}
}
</script>
<html>
<head>
<title>Company Contacts</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#0099FF">
<form name="form1" method="post" runat="server">
<asp:label runat="server" Text="Company Lookup:"></asp:label>
<asp:ListBox id="lbxcomps" runat="server"
AutoPostBack="False" Rows="5" />
<br></br>
<a href="c_update.aspx" title="c_addupdate" target="mainFrame">ADD/UPDATE</a>
<a href="c_details.aspx" title="c_details" target="mainFrame">DETAILS</a>
<a href="c_contacts.aspx" title="c_contacts" target="mainFrame">CONTACTS</a>
<a href="c_jobs.aspx" title="c_jobs" target="mainFrame">JOBS</a>
<br></br>
<br></br>
<table width="100%" border="0">
<tr>
<td colspan="3">
<asp:datagrid ID="newcontact" runat="server" Width="728" Height="234" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1" BackColor="#FFFFFF" CellPadding="4" EnableViewState="false" Visible="false">
<ItemStyle Forecolor="#330099" BackColor="White" />
<headerstyle Font-Bold="true" ForeColor="#FFFFCC" BackColor="#990000" />
<footerstyle ForeColor="#330099" BackColor="#FFFFCC" />
<pagerstyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC" />
</asp:datagrid>
</td>
</tr>
<tr>
<td valign="top">
<asp:label runat="server" Text="Contacts:"></asp:label>
<asp:ListBox id="lbxcontacts" runat="server"
AutoPostBack="True"
Rows="3" />
</td>
<td>
<asp:label ID="lblname" runat="server" Text="Name"></asp:label>
<asp:textbox ID="name" runat="server" />
<p></p>
<asp:label ID="lblposition" runat="server" Text="Position"></asp:label>
<asp:textbox ID="position" runat="server" />
<p></p>
<asp:label ID="lbltel" runat="server" Text="Telephone"></asp:label>
<asp:textbox ID="telephone" runat="server" />
<p></p>
<asp:label ID="lblfax" runat="server" Text="Fax"></asp:label>
<asp:textbox ID="fax" runat="server" />
<p></p>
<asp:label ID="lblmail" runat="server" Text="E-Mail"></asp:label>
<asp:textbox ID="email" runat="server" />
<p></p>
<div align="justify">
<asp:button ID="savecontact" runat="server" Text="SAVE" OnClick="Store" /><a href="c_contacts.aspx">RETURN</a>
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
--------------------------------------------------------------------