ASP.NET

Moderators: None (Apply to moderate this forum)
Number of threads: 1733
Number of posts: 3304

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
variable referencing problem (I think...) Posted by menelaos on 14 Jul 2003 at 3:39 AM
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>
&nbsp;&nbsp;&nbsp;<a href="c_update.aspx" title="c_addupdate" target="mainFrame">ADD/UPDATE</a>
&nbsp;&nbsp;&nbsp;<a href="c_details.aspx" title="c_details" target="mainFrame">DETAILS</a>
&nbsp;&nbsp;&nbsp;<a href="c_contacts.aspx" title="c_contacts" target="mainFrame">CONTACTS</a>
&nbsp;&nbsp;&nbsp;<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>&nbsp;
<asp:textbox ID="name" runat="server" />
<p></p>
<asp:label ID="lblposition" runat="server" Text="Position"></asp:label>&nbsp;
<asp:textbox ID="position" runat="server" />
<p></p>
<asp:label ID="lbltel" runat="server" Text="Telephone"></asp:label>&nbsp;
<asp:textbox ID="telephone" runat="server" />
<p></p>
<asp:label ID="lblfax" runat="server" Text="Fax"></asp:label>&nbsp;
<asp:textbox ID="fax" runat="server" />
<p></p>
<asp:label ID="lblmail" runat="server" Text="E-Mail"></asp:label>&nbsp;
<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>

--------------------------------------------------------------------



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.