Hi!
When I am trying to save I receive this message:
Object reference not set to an instance of an object.
Please have a look to my whole code:
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;
using System.Data.SqlClient;
namespace ElSoft_FactuStocks
{
public partial class Facturacao : Form
{
public Facturacao()
{
InitializeComponent();
}
//DataRowView drv;
System.Data.SqlServerCe.SqlCeConnection con;
System.Data.SqlServerCe.SqlCeDataAdapter da;
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
DataSet ds1;
int MaxRows = 0;
int inc = 0;
private void btnSave_Facturacao_Click(object sender, EventArgs e)
{
DataRow dRow = ds1.Tables["facturacao"].NewRow();
dRow[1] = label10.Text;
/*dRow[1] = nomeClienteFactu.Text;
dRow[2] = artigoFactu.Text;
dRow[3] = leituraCodFactu.Text;
dRow[4] = quantidadeFactu.Text;
dRow[5] = precoFactu.Text;
dRow[6] = totalFactu.Text;*/
//dRow[7] = quantidadeFactu.Text;
//dRow[8] = quantidadeFactu.Text;
ds1.Tables["Facturacao"].Rows.Add(dRow);
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da);
cb.DataAdapter.Update(ds1.Tables["Facturacao"]);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
//btnAddNew.Enabled = true;
//btnSave.Enabled = false;
}
VulpesPosted Apr 28, 2014, 2:57 PM
VulpesPosted Apr 29, 2014, 10:19 AM
IsraelPosted Apr 29, 2014, 10:16 AM
IsraelPosted Apr 28, 2014, 12:42 PM
Now I do simplify my code...But there's something little bit wrong in this code. It's running very well without error message. Then when I click to the button to save it's says "saved". In real NOT!
VulpesPosted Apr 28, 2014, 11:53 AM
The reason why this connection string isn't working:
is because you're using the in-memory database SQL Server Compact Edition, not SQL Server Express.
The System.Data.SqlServerCe namespace contains all the classes for the Compact Edition.
IsraelPosted Apr 28, 2014, 11:37 AM
Why I am saying like that just because when I put this line to test if its gone to fetch records. Its works:
con.ConnectionString = "DataSource=c:\\Users\\INAC\\Documents\\Employees.sdf";
this line doesnt works:
con.ConnectionString = "Data Source=localhost\\sqlexpress;Initial Catalog=master;Integrated Security=True";
Let go slowly because I need your help to resolv it (I passed night without sleep for that).
Then the line that works is inside here in blue:
Only this works as I said:
con.ConnectionString = "DataSource=c:\\Users\\INAC\\Documents\\Employees.sdf";
VulpesPosted Apr 27, 2014, 7:50 PM
You've also closed the connection in the Load handler and so will need to reopen it again (or not close it in the first place) to update the database.
IsraelPosted Apr 27, 2014, 7:40 PM
Hi!
VulpesPosted Apr 27, 2014, 7:20 PM
So there are plenty of candidates for being null here.