473,378 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

databinding and updating sample

I need some sample code showing how to manipulate data in my access database
using C#. This is what Im trying to do:

Dropdownlist with datagrid both bound to datasource. When the drop down list
value changes - the datagrid values change accordingly. I managed to get
this working by setting the currencymanager to the dropdownlist
selectedIndex (which is probably the wrong way to do it, bu the datagrid
seems to update its values correctly)

Im stuck with the Updatecommand, deleteCommand part of things. I set my
update comand to something like this "update Something set somethingElse = ?
WHERE this = ? AND that = ?" I get an error message with the update.

Please opint me in the direction of smoe sample code. My internet connection
runs like a 3 legged dog and I get too frustrated waiting for the inevitable
internet timeout.

Thanks heaps,
Grant
Jan 26 '06 #1
3 1798
Hi,

What is your datasource? Is it a dataset or datatable that you have
filled with the contents of your database table?

If you use a dataset you can edit the data locally in your dataset and
then commit the changes to the database from the dataset.

Jan 26 '06 #2
Hi thanks for the reply,
I believe its both. Here is my code which I cannot get to update, it throws
the error: "No value given for one or more required parameters"

------------------Code start------------------------------
private void OpenDataset()
{
//Connection string
string connection = @"Provider=Microsoft.Jet.OLEDB.4.0; "+
@"Data Source=C:\stock.mdb";

// Setup DB-Connection
OleDbConnection conn = new OleDbConnection(connection);

//SQL Select string
//string query = "SELECT * FROM [Component-Codes]";

// Fill the Dataset with Compnent details, map Default Tablename
// "Table" to "Components".
adapter = new OleDbDataAdapter("SELECT * FROM [Component-Codes]",conn);
adapter.TableMappings.Add("Table","Components");
adapter.Fill(dset);

// Fill the Dataset with Compnent details, map Default Tablename
// "Table" to "Products".
adapter2 = new OleDbDataAdapter("SELECT * FROM [Product-Codes]",conn);
adapter2.TableMappings.Add("Table","Products");
adapter2.Fill(dset);

// Fill the Dataset with Compnent details, map Default Tablename
// "Table" to "components".
adapter3 = new OleDbDataAdapter("SELECT * FROM
[Product-Component-Junction]",conn);

//---------------Test area------------------------
adapter3.UpdateCommand = new OleDbCommand("UPDATE
[Product-Component-Junction] SET Quantity = ? " +
"WHERE [Component-Code] = ? AND [Product-Code] = ?" , conn);

//Add Parameters and set values.
//adapter3.UpdateCommand.Parameters.Add("@Country",O leDbType.VarChar,
15).Value = "UK";

//selectCMD.Parameters.Add("@Country", OdbcType.VarChar, 15).Value =
"UK";
//selectCMD.Parameters.Add("@City", OdbcType.VarChar, 15).Value =
"London";
//adapter3.UpdateCommand = new OleDbCommand("UPDATE
[Product-Component-Junction] SET Quantity = ? " , conn);
//---------------Test area------------------------

adapter3.TableMappings.Add("Table","prod-comp-junction");
adapter3.Fill(dset);

// Establish the Relationship "ProdComp"
// between Products ---< prod-comp-junction
System.Data.DataRelation ProdComp;
System.Data.DataColumn col_Products;
System.Data.DataColumn col_JunctionTable_Prods;
col_Products = dset.Tables["Products"].Columns["Code"];
col_JunctionTable_Prods =
dset.Tables["prod-comp-junction"].Columns["Product-Code"];
ProdComp = new System.Data.DataRelation("ProdComp",col_Products,
col_JunctionTable_Prods);
dset.Relations.Add(ProdComp);

// Establish the Relationship "ProdComp"
// between Components ---< prod-comp-junction
System.Data.DataRelation Component_Product_Junction;
System.Data.DataColumn col_Components;
System.Data.DataColumn col_JunctionTable;
col_Components = dset.Tables["Components"].Columns["Code"];
col_JunctionTable =
dset.Tables["prod-comp-junction"].Columns["Component-Code"];
Component_Product_Junction = new
System.Data.DataRelation("Component_Product_Juncti on",col_Components,col_JunctionTable);
dset.Relations.Add(Component_Product_Junction);

dsView = dset.DefaultViewManager;

// Grid Databinding
dataGrid1.DataSource = dsView;
dataGrid1.DataMember = "Products.ProdComp";

//--------------------Combobox---------------
comboBox1.DataSource = dset.Tables["Products"];
comboBox1.DisplayMember = "Code";
comboBox1.ValueMember = "Products.Code";

//--------------------Combobox---------------

}

private void openDataSet()
{
string connection = @"Provider=Microsoft.Jet.OLEDB.4.0; "+
@"Data Source=C:stock.mdb";
string query = "SELECT * FROM [Component-Codes]";

OleDbConnection conn = new OleDbConnection(connection);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(query, conn);
adapter.Fill(dset);

}

private void BindControls()
{
comboBox1.DataSource = dset.Tables["Table"];
comboBox1.DisplayMember = "Code";

}
private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)
{

CurrencyManager cm =
(CurrencyManager)this.BindingContext[dsView,"Products"];
cm.Position = comboBox1.SelectedIndex;
}

private void comboBox1_BindingContextChanged(object sender, EventArgs e)
{
CurrencyManager cm =
(CurrencyManager)this.BindingContext[dset,"Products"];
cm.Position = cm.Count + 1;

}

private void btnUpdate_Click(object sender, System.EventArgs e)
{
try
{
//adapter.Update(dset);
//adapter2.Update(dset);
adapter3.Update(dset);
}
catch(Exception exc)
{
catchError(exc);
}
}
-----------------------Code end-----------------------------------

"Emma Middlebrook" <em******@hotmail.com> wrote in message
news:11*********************@g14g2000cwa.googlegro ups.com...
Hi,

What is your datasource? Is it a dataset or datatable that you have
filled with the contents of your database table?

If you use a dataset you can edit the data locally in your dataset and
then commit the changes to the database from the dataset.

Jan 27 '06 #3
Have you tried using the OleDbCommandBuilder to help you construct the
other statements? That might help you eliminate the possibility that
your SQL syntax might be wrong..?

The error seems to think that you have not supplied enough parameters
for the update..

Here's a sample from msdn on how it works:

OleDbConnection myConn = new OleDbConnection(myConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery,
myConn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(myDataAdapter);

myConn.Open();

DataSet ds = new DataSet();
myDataAdapter.Fill(ds, myTableName);

//code to modify data in DataSet here

//Without the OleDbCommandBuilder this line would fail
myDataAdapter.Update(ds, myTableName);

myConn.Close();

return ds;

Jan 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael | last post by:
I've hooked up a TextBox's Text property with the property of a class. However, when invalid data is entered, the data binding updates the object immediately. Is there anyway to prevent this from...
3
by: Luis Esteban Valencia | last post by:
Hi, the following code displays a search result in a datagrid and is working without problems. But I think the way I have done is not correct especially databind(). could anyone help me to...
0
by: Kevin Hodgson | last post by:
I have a ComboBox and a TextBox bound to a Dataset/datatable returned from a SQL Database. The databinding is set in Design Mode. Everything works fine on my initial fill, and I get the correct...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
8
by: Joey Chömpff | last post by:
L.S., Hello is there a way to implement 2-way databinding without using the datasources from dotnet 2.0. Why would you ask? Now that's simple. I've created an object model with BusinessObjects...
2
by: John Rusk | last post by:
Hi, Does the databinding support in ASP.NET 2.0 support two way databinding to datasets? I have a dataset, which is populated with data. I just want to bind the UI to it. I'm struggling to...
8
by: Dirk | last post by:
Hello, I have a problem to use databinding with my business layer classes. My data class does not have simple properties (string, int or datetime), instead, all my properties are objects of the...
11
by: =?Utf-8?B?R29rdWw=?= | last post by:
I am struck up with a problem and want anyone here to help me out. I am a beginner in .NET trying to learng DataBinding concepts. I have binded 4 text boxes with a dataset but when I say...
7
by: Vlado Jasovic | last post by:
Hello, I'm using typed dataset for databinding to windows controls and I'm having some problems. I'm trying to move all business logic to datatable column_changing events and the problem that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.