473,550 Members | 2,417 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1823
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=Micr osoft.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 OleDbDataAdapte r("SELECT * FROM [Component-Codes]",conn);
adapter.TableMa ppings.Add("Tab le","Components ");
adapter.Fill(ds et);

// Fill the Dataset with Compnent details, map Default Tablename
// "Table" to "Products".
adapter2 = new OleDbDataAdapte r("SELECT * FROM [Product-Codes]",conn);
adapter2.TableM appings.Add("Ta ble","Products" );
adapter2.Fill(d set);

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

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

//Add Parameters and set values.
//adapter3.Update Command.Paramet ers.Add("@Count ry",OleDbType.V arChar,
15).Value = "UK";

//selectCMD.Param eters.Add("@Cou ntry", OdbcType.VarCha r, 15).Value =
"UK";
//selectCMD.Param eters.Add("@Cit y", OdbcType.VarCha r, 15).Value =
"London";
//adapter3.Update Command = new OleDbCommand("U PDATE
[Product-Component-Junction] SET Quantity = ? " , conn);
//---------------Test area------------------------

adapter3.TableM appings.Add("Ta ble","prod-comp-junction");
adapter3.Fill(d set);

// Establish the Relationship "ProdComp"
// between Products ---< prod-comp-junction
System.Data.Dat aRelation ProdComp;
System.Data.Dat aColumn col_Products;
System.Data.Dat aColumn col_JunctionTab le_Prods;
col_Products = dset.Tables["Products"].Columns["Code"];
col_JunctionTab le_Prods =
dset.Tables["prod-comp-junction"].Columns["Product-Code"];
ProdComp = new System.Data.Dat aRelation("Prod Comp",col_Produ cts,
col_JunctionTab le_Prods);
dset.Relations. Add(ProdComp);

// Establish the Relationship "ProdComp"
// between Components ---< prod-comp-junction
System.Data.Dat aRelation Component_Produ ct_Junction;
System.Data.Dat aColumn col_Components;
System.Data.Dat aColumn col_JunctionTab le;
col_Components = dset.Tables["Components "].Columns["Code"];
col_JunctionTab le =
dset.Tables["prod-comp-junction"].Columns["Component-Code"];
Component_Produ ct_Junction = new
System.Data.Dat aRelation("Comp onent_Product_J unction",col_Co mponents,col_Ju nctionTable);
dset.Relations. Add(Component_P roduct_Junction );

dsView = dset.DefaultVie wManager;

// Grid Databinding
dataGrid1.DataS ource = dsView;
dataGrid1.DataM ember = "Products.ProdC omp";

//--------------------Combobox---------------
comboBox1.DataS ource = dset.Tables["Products"];
comboBox1.Displ ayMember = "Code";
comboBox1.Value Member = "Products.Code" ;

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

}

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

OleDbConnection conn = new OleDbConnection (connection);
OleDbDataAdapte r adapter = new OleDbDataAdapte r();
adapter.SelectC ommand = new OleDbCommand(qu ery, conn);
adapter.Fill(ds et);

}

private void BindControls()
{
comboBox1.DataS ource = dset.Tables["Table"];
comboBox1.Displ ayMember = "Code";

}
private void comboBox1_Selec tedIndexChanged (object sender,
System.EventArg s e)
{

CurrencyManager cm =
(CurrencyManage r)this.BindingC ontext[dsView,"Product s"];
cm.Position = comboBox1.Selec tedIndex;
}

private void comboBox1_Bindi ngContextChange d(object sender, EventArgs e)
{
CurrencyManager cm =
(CurrencyManage r)this.BindingC ontext[dset,"Products"];
cm.Position = cm.Count + 1;

}

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

"Emma Middlebrook" <em******@hotma il.com> wrote in message
news:11******** *************@g 14g2000cwa.goog legroups.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 OleDbCommandBui lder 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);
OleDbDataAdapte r myDataAdapter = new OleDbDataAdapte r();
myDataAdapter.S electCommand = new OleDbCommand(my SelectQuery,
myConn);
OleDbCommandBui lder cb = new OleDbCommandBui lder(myDataAdap ter);

myConn.Open();

DataSet ds = new DataSet();
myDataAdapter.F ill(ds, myTableName);

//code to modify data in DataSet here

//Without the OleDbCommandBui lder this line would fail
myDataAdapter.U pdate(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
4626
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 happening? Or should I just re-assign the value to the object directly in this case? Thanks, Michael
3
1178
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 rearange it? Sub grdProfilesResult_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) grdProfilesResult.EditItemIndex =...
0
980
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 list of items in the combobox, and the textbox which is bound to a second column in the same table, changes to the appropriate value when a new item...
5
12501
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 update the datasource - awesome, but I have an issue with updating from a different thread. Here is my datasource, a person class that raises the...
8
1824
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 and I don't want to write an separate provider layer for the datasources. This is because I loose all the benefits of my objectmodel. Are there...
2
1178
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 see any way to do this, and I see this post, which seems to suggest that it cannot be done (unless you write an additional adapter class):...
8
2078
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 generic type Field<T> (see sample code). public class Employee { public Field<stringForename
11
1784
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 adapter.update it gives me 0 records updated! I am not getting any exceptions. Below is the complete code. Someone please help me out. using System; using...
7
2584
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 I'm having is when I change some other column, control value doesn't update immediately until I call endcurrentedit on bindingmanager which doesnt...
0
7563
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7490
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7765
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7520
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7846
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3519
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1096
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
802
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.