472,992 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

database wont update

D
I am trying to add records to a database and getting no errors but the
databse is not updated.
Not sure what is wrong maybe in my function I'm losing some thing?
What do you think? My code is below.

Thanks

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using System.Data.Odbc;
using System.Collections;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
OdbcConnection dbconn = new
OdbcConnection("Provider=MSDASQL.1;DRIVER={Microso ft Access Driver
(*.mdb)};DBQ=c:\\hedgefunds\\db1.mdb;DATABASE=db1; UID=root;PWD=;
OPTION=1;");
dbconn.Open();
OdbcDataAdapter dbrs = new OdbcDataAdapter("Select * From
MyTable;", dbconn);
OdbcCommandBuilder builder = new OdbcCommandBuilder(dbrs);
DataSet dbADOrs = new DataSet();

InitializeComponent();
dbrs.MissingSchemaAction = MissingSchemaAction.AddWithKey;
dbrs.Fill(dbADOrs);
OpenReturnFile(dbrs, dbADOrs);
}
public void OpenReturnFile(DbDataAdapter dbrs, DataSet dbADOrs)
{
StreamReader sfp;
sfp = File.OpenText("c:\\temp\\updatenewfile.txt");
string buffer;
string[] fields;
char[] delims={'\t'};
while (sfp.EndOfStream == false)
{
buffer = sfp.ReadLine();
fields=buffer.Split(delims,
StringSplitOptions.RemoveEmptyEntries);
UpdateinDB(dbrs, dbADOrs, fields);

}

}
public void UpdateinDB(DbDataAdapter dbrs, DataSet dbADOrs,
String[] fields)
{
DataTable dt = dbADOrs.Tables[0];
DataRow rec = dt.NewRow();
MessageBox.Show(dt.Columns[0].ToString());
if (fields[0] != "fundname")//file has header so ignore it.
{
rec["name"] = fields[0];
rec["date"] = fields[1];
rec["corr"] = fields[2];

try
{
dbrs.Update(dbADOrs);//no update to the database occurs
here no exceptions triggered
}
catch (Exception e)
{
StreamWriter fout = new
StreamWriter("c:\\temp\\status.txt", true);
fout.WriteLine("Error at " + fields[0].ToString()+ "
" + e.InnerException.ToString());
fout.Close();
}
}
}
}

}

Jun 27 '08 #1
1 1180
D wrote:
I am trying to add records to a database and getting no errors but the
databse is not updated.
Not sure what is wrong maybe in my function I'm losing some thing?
What do you think? My code is below.
<snip>
OdbcConnection dbconn = new
OdbcConnection("Provider=MSDASQL.1;DRIVER={Microso ft Access Driver
(*.mdb)};DBQ=c:\\hedgefunds\\db1.mdb;DATABASE=db1; UID=root;PWD=;
OPTION=1;");
Your problem's right there: you're using Access. Use a real database
instead. SQL Server Express is free. So's SQL Server Compact Edition if you
need the ability to exchange databases as files. Snarking aside, though...
public void UpdateinDB(DbDataAdapter dbrs, DataSet dbADOrs,
String[] fields)
{
DataTable dt = dbADOrs.Tables[0];
DataRow rec = dt.NewRow();
MessageBox.Show(dt.Columns[0].ToString());
if (fields[0] != "fundname")//file has header so ignore it.
{
rec["name"] = fields[0];
rec["date"] = fields[1];
rec["corr"] = fields[2];

try
{
dbrs.Update(dbADOrs);//no update to the database occurs
here no exceptions triggered
You forgot to call .AddRow(). This always trips up newcomers (I speak from
experience), but .NewRow() doesn't actually add a new row to the table, it
just creates one based on the table's scheme.

See http://msdn.microsoft.com/library/z16c79x4 for a sample.

--
J.
Jun 27 '08 #2

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

Similar topics

25
by: cory | last post by:
Hi, I have an Access database and am having an ASP.NEt application written for it. It is almost complete. I have a hosting company that I signed up with a month ago but before I did anything I...
6
by: Brett | last post by:
Not sure what the problem is here... Trying to update from a datagrid to an access database using vb.net... Its not updating the database but Im not getting any errors... Here is my code... ...
25
by: Mark Harrison | last post by:
I would like to try and build a queuing mechanism on top of Postgresql. Imagine an application where a large number of processes generate images and queue up thumbnail requests. A smaller number...
2
by: TJ | last post by:
Hi All, I am having some trouble. I have created a database via the new database option inside VWD2005. Then and table or two. I have been able to perform INSERT and SELECT operations on...
1
by: eduardoben | last post by:
I use a tableadapter with the sql string = select * fom table where field=? I load the desired record in a form and populate textboxes but after i modify them when i use tableadapter.update it...
2
by: umeshpotdar | last post by:
hi frdz i want to do synchronization of database the thing is like that, i have two databases which is deve. in sql server 2005 (both are same) suppose one is at Delhi and another is at Mumbai....
2
by: Bomac8 | last post by:
I have a stored procedure: CREATE PROCEDURE ( @residentID int OUTPUT, @resNames (100), @LName (100), @resAddress (50), @resEMail (50), @resPhone (50),
1
by: =?Utf-8?B?UmljaA==?= | last post by:
Private Sub UpdateTblHistory() Dim strSql As String strSql = "Update tbl_History set SubscrID = @SubscrID Where ID = @ID" da.UpdateCommand.CommandText = strSql...
30
by: fniles | last post by:
On my machine in the office I change the computer setting to English (UK) so the date format is dd/mm/yyyy instead of mm/dd/yyyy for US. This problem happens in either Access or SQL Server. In the...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

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.