473,396 Members | 1,827 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,396 software developers and data experts.

Adding a row to a database

I keep getting "The ConnectionString property has not been initialized." on the line da.Update(ds1. "Mail")

I've tried wrapping the section in a open and close (data base connection) and now I get the following error message: Syntax error in INSERT INTO statement. on this segment of code: da.Update(ds1, "Mail");

I put in a break point and checked and the DataSet has been updated with the new information

I'm able to read from the database, just can't add a new row to it...

Using C# 2005

Expand|Select|Wrap|Line Numbers
  1.  
  2.  System.Data.OleDb.OleDbConnection con;
  3.         DataSet ds1;
  4.         System.Data.OleDb.OleDbDataAdapter da;
  5.         int intMaxRows = 0;
  6.         int intCurrentRow = 0;
  7.  
  8.         private void Form1_Load(object sender, EventArgs e)
  9.         {
  10.             con = new System.Data.OleDb.OleDbConnection();
  11.             ds1 = new DataSet();
  12.  
  13.             openConnection();
  14.  
  15.             string sql = "Select * From EmailLog";
  16.             da = new System.Data.OleDb.OleDbDataAdapter(sql, con);
  17.  
  18.             da.Fill(ds1, "Mail");
  19.             NavigateRecords();
  20.             intMaxRows = ds1.Tables["Mail"].Rows.Count;
  21.  
  22.             closeConnection();
  23.  
  24.         }
  25.  
  26.         private void NavigateRecords()
  27.         {
  28.             DataRow dRow = ds1.Tables["Mail"].Rows[intCurrentRow];
  29.             txtDate.Text = dRow.ItemArray.GetValue(1).ToString();
  30.             txtFrom.Text = dRow.ItemArray.GetValue(2).ToString();
  31.             txtSubject.Text = dRow.ItemArray.GetValue(3).ToString();
  32.             txtEmailMsg.Text = dRow.ItemArray.GetValue(4).ToString();
  33.             txtMessageId.Text = dRow.ItemArray.GetValue(5).ToString();
  34.  
  35.         }
  36. private void btnSave_Click(object sender, EventArgs e)
  37.         {
  38.             openConnection();
  39.  
  40.             System.Data.OleDb.OleDbCommandBuilder cb;
  41.             cb = new System.Data.OleDb.OleDbCommandBuilder(da);
  42.  
  43.             DataRow dRow = ds1.Tables["Mail"].NewRow();
  44.  
  45.             dRow[1] = txtDate.Text;
  46.             dRow[2] = txtFrom.Text;
  47.             dRow[3] = txtSubject.Text;
  48.             dRow[4] = txtEmailMsg.Text;
  49.             dRow[5] = txtMessageId.Text;
  50.  
  51.             ds1.Tables["Mail"].Rows.Add(dRow);
  52.  
  53.             intMaxRows = intMaxRows + 1;
  54.             intCurrentRow = intMaxRows - 1;
  55.  
  56.             da.Update(ds1, "Mail"); //Not Working
  57.  
  58.             MessageBox.Show("New Data Added!");
  59.  
  60.             closeConnection();  
  61.         }
  62.         private void openConnection()
  63.         {
  64.             con.ConnectionString = ("provider=Microsoft.Jet.OLEDB.4.0;" + "data source = C:\\PaymentFileTracker.mdb");
  65.  
  66.             con.Open();
  67.             MessageBox.Show("Connection to Data Base Open");
  68.         }
  69.         private void closeConnection()
  70.         {
  71.             con.Close();
  72.             MessageBox.Show("Connection to Data Base is Closed");
  73.  
  74.             con.Dispose();
  75.         }
  76.  
Feb 24 '10 #1
4 2553
Bassem
344 100+
Hi,

Error #1:
It is obvious that you didn't set the connection string to a valid one to the command builder. Which I can not judge here.

Error #2:
What is the schema of your table. I think Id is a numeric and Date is a DateTime. But here you add five strings as a row!
Perhaps, you should cast these values to meet the table schema.

Thanks,
Bassem
Feb 25 '10 #2
Actually they are all strings on the database, the date I'm reading/writing actually reads "Fri, 19 Feb 2010 10:22:34 -0500" for example. Though I will eventually change that once I get everything working to just a regular DateTime field.

As far as the first error goes, I thought that the commandbuilder reconnects you to the database and all you have to do is pass it a data adapter which I do with
cb = new System.Data.OleDb.OleDbCommandBuilder(da);
When I get that error the OpenConnection and CloseConnection in private void btnSave_Click are commented out.
Feb 25 '10 #3
Bassem
344 100+
I don't follow you, but I think my suggestions were not right.

One last thing, did you set the Id in your table to be an index - auto increased - ?
Feb 25 '10 #4
The columns in the table are as follows:
EmailID (Auto Number), DateReceived, From, Subject, Message, MessageID (rest are all strings)

when I get the error "The ConnectionString property has not been initialized." the code reads:

# private void btnSave_Click(object sender, EventArgs e)
# {
# //openConnection();
#
# System.Data.OleDb.OleDbCommandBuilder cb;
# cb = new System.Data.OleDb.OleDbCommandBuilder(da);
#
# DataRow dRow = ds1.Tables["Mail"].NewRow();
#
# dRow[1] = txtDate.Text;
# dRow[2] = txtFrom.Text;
# dRow[3] = txtSubject.Text;
# dRow[4] = txtEmailMsg.Text;
# dRow[5] = txtMessageId.Text;
#
# ds1.Tables["Mail"].Rows.Add(dRow);
#
# intMaxRows = intMaxRows + 1;
# intCurrentRow = intMaxRows - 1;
#
# da.Update(ds1, "Mail"); //Not Working
#
# MessageBox.Show("New Data Added!");
#
# // closeConnection();
# }
That's what I meant, sorry for any confusion
Feb 25 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: mark | r | last post by:
ok, so i now know how to add, edit, delete and search for products (havent figured out categorisation out yet tho) - how do i upload an image and save the file name to the database as part of the...
10
by: Eric Petruzzelli | last post by:
If I fill my dataset and there is no data. The dataset is still created with zero rows (all columns are there). When I add my first row using the script below, it takes over 2 seconds to add??? If...
2
by: btober | last post by:
I run the following script to export some data from my development database and then update or insert the records into to the quality assurance testing database, but I get a warning notice that I...
11
by: Bobbak | last post by:
Hello All, I have these tables (lets call it ‘EmpCalls', ‘EmpOrders', and ‘Stats') that each contain the list of EmployeeIDs, I want to be able to create a Module in which I could call in my VB...
7
by: Alex | last post by:
Hi all, I've found a module that I think will help me combine fields properly, but I'm unsure how to add or use it with Access 2000. Below is the module I'd like to add: ...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
3
by: Robin Thomas | last post by:
I am fairly new to ASP.NET so I think I am missing something fundamental. Anyway, quite often I am pulling data from a database, but then I need to use that data to produce more data. A simple...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
0
by: Lucas Tam | last post by:
Hi all, I posted a message a couple minutes early and I'm not sure if it was too clear. I'm interested in adding custom messages to the validation control - is there an easy way to do this?...
6
by: teddysnips | last post by:
I'm having trouble adding Access 2003 databases to Sourcesafe. I go to Tools, Sourcesafe, Add Database to Sourcesafe... at which point a message box appears saying "This database must be closed...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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...
0
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...
0
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,...

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.