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" %>
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);
}
}
Company Contacts
ADD/UPDATE DETAILS CONTACTS JOBS
--------------------------------------------------------------------