473,698 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

what am I doing wrong ?

Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using the form controls. I"m trying
to do it solely thru code. You can safely assume I have an access datsabase
with one table called who with three columns(fields) .
where did i go wrong ?

OleDbConnection con;
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
con.Close();

thanx in advance for all your help

Nov 10 '06 #1
16 1866
Just looking at it quickly, it looks like you're connection isn't open.
I haven't done much with the oledbconnection object but I think that's
part of your problem. It may not be all of it though.

Nov 10 '06 #2
I added con.open()
it didn'y make any difference.
like I said.
I get no errors ! It completes the proccess. when I check the database there
is no data.

"Doug" wrote:
Just looking at it quickly, it looks like you're connection isn't open.
I haven't done much with the oledbconnection object but I think that's
part of your problem. It may not be all of it though.

Nov 10 '06 #3
Slimshin,

Your code is not complete to update, you can add this,
OleDbConnection con;
try
{
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
SQLCommandBuild er cmd = new SQLCommandBuild er();
cm = cmd(OleDBbDataA dapter);
OleDbDataAdapte r.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
finnaly
{
con.close;
}

Now you can see what goes wrong if that still happen (I have typed this
direct in this message and did a long time not use the commandbuilder) .

As it is working you can refactor your code, by instance using "using" for
the connection does make it look nicer.

I hope this helps,

Cor


con.Close();

Nov 10 '06 #4
I still get no errors.
why is it not ready to update?
doesn't the call do row.add do the update ?
as a side issue using the try catch con.close is out of scope genrating
compiler error.

"Cor Ligthert [MVP]" wrote:
Slimshin,

Your code is not complete to update, you can add this,
OleDbConnection con;
try
{
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
SQLCommandBuild er cmd = new SQLCommandBuild er();
cm = cmd(OleDBbDataA dapter);
OleDbDataAdapte r.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
finnaly
{
con.close;
}

Now you can see what goes wrong if that still happen (I have typed this
direct in this message and did a long time not use the commandbuilder) .

As it is working you can refactor your code, by instance using "using" for
the connection does make it look nicer.

I hope this helps,

Cor


con.Close();


Nov 10 '06 #5
Out of scope while you have placed the declaration outside above the method,
strange

Cor

"SLIMSHIM" <SL******@discu ssions.microsof t.comschreef in bericht
news:1A******** *************** ***********@mic rosoft.com...
>I still get no errors.
why is it not ready to update?
doesn't the call do row.add do the update ?
as a side issue using the try catch con.close is out of scope genrating
compiler error.

"Cor Ligthert [MVP]" wrote:
>Slimshin,

Your code is not complete to update, you can add this,
OleDbConnection con;
try
{
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
SQLCommandBuil der cmd = new SQLCommandBuild er();
cm = cmd(OleDBbDataA dapter);
OleDbDataAdapt er.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show (ex.ToString()) ;
}
finnaly
{
con.close;
}

Now you can see what goes wrong if that still happen (I have typed this
direct in this message and did a long time not use the commandbuilder) .

As it is working you can refactor your code, by instance using "using"
for
the connection does make it look nicer.

I hope this helps,

Cor


con.Close();



Nov 10 '06 #6
Hi,

If I understand correctly, you are expecting to see the new row appear in
your database table?

This code will not do that.
You are creating a dataset, filling it with data from your database, and
adding a new row to it. However, the DataSet is (by design) disconnected
from the database, and you are doing nothing to write the changed dataset
back to your database.
Your will need to write some more code to do this. One way is to give your
DataAdapter 'Insert', 'Update' and 'Delete' queries in addition to the
'Select' query you have already written for it. You can create these queries
by hand, or you can look at the CommandBuilder object.
You will also need to call the .Update method on your DataAdapter once you
have made the required changes.

Hope this helps.

Chris.

"SLIMSHIM" <SL******@discu ssions.microsof t.comwrote in message
news:EE******** *************** ***********@mic rosoft.com...
Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using the form controls. I"m trying
to do it solely thru code. You can safely assume I have an access
datsabase
with one table called who with three columns(fields) .
where did i go wrong ?

OleDbConnection con;
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
con.Close();

thanx in advance for all your help

Nov 10 '06 #7
Hi,

DataSets are "disconnect ed" from the data source that was used to fill them.
In other words, they are only in-memory representations of the data in your
data store.

To update the data store with the changes in the DataSet you can call the
Update method on your data adapter and pass it the DataSet.

In order for the data adapter to successfully update the database, you must
supply the command for the update.

As Cor mentioned, you should really construct "con" in a C# "using" statement
to ensure that the connection is closed in the case of an exception being
thrown. You don't want to leave connections open. (Especially not in Access,
which I believe is really restrictive in the amount of concurrency it
supports):

DataSet ds = new DataSet();

using (OleDbConnectio n conn = new OleDbConnection (
testacc.Propert ies.Settings.De fault.mytestCon nectionString))
{
using (OleDbDataAdapt er adp = new OleDbDataAdapte r(
"SELECT * FROM who", conn))
{
// The following line isn't normally required.
// adp.Fill opens the connection and closes it before returning,
// but since we want it to remain open for the next
// operation (Update) we'll open it now.
// adp.Fill won't close it in this case.
conn.Open();

adp.Fill(ds, "who");

DataRow row = ds.Tables["who"].NewRow();

row["name"] = "Joe";
row["address"] = "1 csharp road :)";
row["age"] = 25;

ds.Tables[0].Rows.Add(row);

// in order to update the database we must tell the adpater how:

using (OleDbCommand comm = new OleDbCommand(
"INSERT INTO who (name, address, age) VALUES (pName, pAddress,
pAge)", conn))
{
comm.Parameters .Add("pName", OleDbType.VarCh ar).SourceColum n =
"name";
comm.Parameters .Add("pAddress" , OleDbType.VarCh ar).SourceColum n =
"address";
comm.Parameters .Add("pAge", OleDbType.Numer ic).SourceColum n =
"age";

// because we have added a new row we must set the InsertCommand
adp.InsertComma nd = comm;

// update the database
adp.Update(ds, "who");
}
}
}

--
Dave Sexton

"SLIMSHIM" <SL******@discu ssions.microsof t.comwrote in message
news:EE******** *************** ***********@mic rosoft.com...
Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using the form controls. I"m trying
to do it solely thru code. You can safely assume I have an access datsabase
with one table called who with three columns(fields) .
where did i go wrong ?

OleDbConnection con;
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
con.Close();

thanx in advance for all your help

Nov 10 '06 #8

"Dave Sexton" <dave@jwa[remove.this]online.comskrev i en meddelelse
news:%2******** **********@TK2M SFTNGP04.phx.gb l...
As Cor mentioned, you should really construct "con" in a C# "using"
statement to ensure that the connection is closed in the case of an
exception being thrown. You don't want to leave connections open.
(Especially not in Access, which I believe is really restrictive in the
amount of concurrency it supports):
Where I work I have been told that we should use try-catch-finally, and
close the connection in the finally, because we can't rely on the dispose
method of the connection to have been coded correctly...
Nov 10 '06 #9
Hi Peter,
>As Cor mentioned, you should really construct "con" in a C# "using"
statement to ensure that the connection is closed in the case of an
exception being thrown. You don't want to leave connections open.
(Especially not in Access, which I believe is really restrictive in the
amount of concurrency it supports):

Where I work I have been told that we should use try-catch-finally, and
close the connection in the finally, because we can't rely on the dispose
method of the connection to have been coded correctly...
The compiler uses a try..finally block when the using statement is compiled.
In the finally block, Dispose is called on the object specified in the using
statement. The object specified in the using statement must be implicitly
convertible to IDisposable or a compile-time error will occur. The using
statement is just an elegant shortcut to the same code as this:

OleDbConnection conn = new OleDbConnection (...);

try
{
// statements inside using block are compiled here
}
finally
{
conn.Dispose();
}

I prefer the using statement over the above code simply because it's more
legible, IMO.

The using statement doesn't compile a "catch" statement. If you except to be
able to handle a particular exception in code, you must add your own
try..catch around the using statement, or better yet, just use your own
try..catch..fin ally:

OleDbConnection conn = new OleDbConnection (...);

try
{
// use conn
}
catch (OleDbException ex)
{
// handle exception
}
finally
{
conn.Dispose();
}

--
Dave Sexton
Nov 10 '06 #10

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

Similar topics

125
14756
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from software giant such as Microsoft SQL Server, Oracle, and Sybase? Is PostgreSQL reliable enough to be used for high-end commercial application? Thanks
121
10080
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode support IDEs are DreamWeaver 8 and Zend PHP Studio. DreamWeaver provides full support for Unicode. However, DreamWeaver is a web editor rather than a PHP IDE. It only supports basic IntelliSense (or code completion) and doesn't have anything...
11
2032
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure containing pointers into a "flattened" bit stream. Here is my code (it dosen't work). I would be grateful for any feedback that helps fix this. My intention
46
4217
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do believe MSFT should do to improve C#, however. I know that in the "Whidbey" release of VS.NET currently
13
5040
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
2
2706
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update button, seems simple. I am using ALL visual controls (supposedly to simplify things. If I was not using the visual controls and calling an ExecuteNonQuery no prob. Please look at my code and tell me what I am doing wrong. Also, what are the advatages...
16
1985
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. 1. constructor 2.Destructor 3.Copy constructor 4. Assignment operator
8
2065
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the vertices and can do this multiple times on a sinlge vertex cluster. The problem I have been encoutering is that, if I select a second vertex cluster and try to edit that , the program crashes.
0
1322
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the child controls (just a literal for testing) of my control is not being added to the page. Could someone tell me what am I doing wrong?
10
1795
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
8604
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9028
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8861
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2001
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.