Open you database in Access. View the table with "Design View" (right click
the table).
See if the "Required" is set to "yes". Change this to "no" see if this
works.
If not you can set a default value to eg. "0" with the "Default Value"
property.
I'm not sure what your german error is in english... (I'm not used to read
SQL errors in german.)
Regards,
Lars-Inge Tønnessen
Hi Lars-Inge!
I had no required fields in the access-file, because I handle the required
Data in my WebForm (RequiredVieldValidator). But I set some fields to
"number" and some fields to "text". I dind't know, that a number-field needs
an entry if you send the data via sql-statement. Under Access I can leaf the
field blank, but if I send the data via sql, I have to send something. But I
dind't want to send a '0' so I changed all fields to type "text" because I
select the entered data over the WebForm (RegularExpressionValidator). And
now it seems, that all both errors are away...
By the way, what is the best way to send data to an access-file?
Now I use the follwoing system, but I think there must be a better one:
***
// Diese Methode fügt die gesammelten Daten in die Datenbank ein. Achtung:
Diese Methode
// benötigt "System.Data.OleDb.*"!
private void DatenAnDatenbankSenden()
{
// Einstellungen für den Datenbankzugriff festelgen:
String connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
connectionString += "Data Source=" + datei + ";";
// SQL-Befehl, der dan die Datenbank gesendet werden soll, erstellen:
String sql = "INSERT INTO ";
sql += this.monat + " ";
sql += "(";
sql += "Datum";
sql += ",Uhrzeit";
sql += ",Programmname";
sql += ",Anrede";
sql += ",Titel";
sql += ",Name1";
sql += ",Name2";
sql += ",Ansprechpartner";
sql += ",Strasse";
sql += ",Land";
sql += ",Plz";
sql += ",Ort";
sql += ",Ortsvorwahl";
sql += ",Telefon";
sql += ",Fax";
sql += ",EMail";
sql += ",Homepage";
sql += ",Betriebssystem";
sql += ")";
sql += " VALUES ";
sql += "('";
sql += this.datum;
sql += "','" + this.uhrzeit;
sql += "','" + this.pName1;
sql += "','" + this.anrede;
sql += "','" + this.titel;
sql += "','" + this.name1;
sql += "','" + this.name2;
sql += "','" + this.ansprechpartner;
sql += "','" + this.strasse;
sql += "','" + this.land;
sql += "','" + this.plz;
sql += "','" + this.ort;
sql += "','" + this.ortsvorwahl;
sql += "','" + this.telefon;
sql += "','" + this.fax;
sql += "','" + this.email;
sql += "','" + this.homepage;
sql += "','" + this.betriebssystem;
sql += "')";
// Neues OleDBConnection Objekt "conn" erstellen:
OleDbConnection conn = new OleDbConnection(connectionString);
// Neues OleDbCommand Objekt "cmd" erstellen
OleDbCommand cmd = new OleDbCommand(sql, conn);
// Mit try/catch/finally werden Fehler abgefangen:
try
{
// Datenbankverbindung öffnen:
conn.Open();
// SQL-Befehl an die Datenbank senden:
cmd.ExecuteNonQuery();
}
// Wenn ein Fehler aufgetreten ist, dann muss dieser abgefangen werden:
catch (Exception ex)
{
// Wenn ein Fehleraufgetreten ist, die Fehlermeldung an die Variable
// "fehlerMeldung" übergeben:
this.fMailText = ex.get_Message();
// Und anschließend eine Fehler-E-Mail senden:
this.SendeEMail(fMailAn, fMailBetreff, fMailText);
}
// Der finally-Block wird immer abgearbeitet, egal ob ein Fehler
// aufgetreten ist oder nicht:
finally
{
// Resourcen wieder freigeben:
cmd.Dispose();
// Datenbankverbindung schließen:
conn.Close();
}
}
***
Lars-Inge Tønnessen [VJ# MVP] - 25 Jun 2005 22:27 GMT
> By the way, what is the best way to send data to an access-file?
By using the SQL statement INSERT INTO, and executeNonQuery just like you
do.
If you don't want to insert values into a database you must enable NULL's in
the datafield in the table. When you insert data into the table, don't use
the filed in the INSERT INTO statement. You can do that by building the SQL
statement dynamically by using string handling. (Please use
System.Text.StringBuilder for speed).
Here is an example I just wrote. This is a J# win app exe for simplicity.
Open a new J# "Windows application" and replace the code in the "Form1" with
this code. This code will produce the SQL statement dynamically.
The code is straight forward, it could also be done more dynamically by Eg.
using the tags in the GUI.
package DynamicSQL;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.TextBox textBox4;
private System.Windows.Forms.TextBox textBox5;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}
#region Windows Form Designer generated code
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.textBox4 = new System.Windows.Forms.TextBox();
this.textBox5 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.set_Location(new System.Drawing.Point(30, 24));
this.textBox1.set_Name("textBox1");
this.textBox1.set_TabIndex(0);
this.textBox1.set_Text("");
//
// textBox2
//
this.textBox2.set_Location(new System.Drawing.Point(30, 56));
this.textBox2.set_Name("textBox2");
this.textBox2.set_TabIndex(1);
this.textBox2.set_Text("");
//
// textBox3
//
this.textBox3.set_Location(new System.Drawing.Point(30, 88));
this.textBox3.set_Name("textBox3");
this.textBox3.set_TabIndex(2);
this.textBox3.set_Text("");
//
// textBox4
//
this.textBox4.set_Location(new System.Drawing.Point(30, 120));
this.textBox4.set_Name("textBox4");
this.textBox4.set_TabIndex(3);
this.textBox4.set_Text("");
//
// textBox5
//
this.textBox5.set_Location(new System.Drawing.Point(30, 152));
this.textBox5.set_Name("textBox5");
this.textBox5.set_TabIndex(4);
this.textBox5.set_Text("");
//
// button1
//
this.button1.set_Location(new System.Drawing.Point(43, 192));
this.button1.set_Name("button1");
this.button1.set_TabIndex(5);
this.button1.set_Text("button1");
this.button1.add_Click( new System.EventHandler(this.button1_Click) );
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(160, 230));
this.get_Controls().Add(this.button1);
this.get_Controls().Add(this.textBox5);
this.get_Controls().Add(this.textBox4);
this.get_Controls().Add(this.textBox3);
this.get_Controls().Add(this.textBox2);
this.get_Controls().Add(this.textBox1);
this.set_Name("Form1");
this.set_Text("Form1");
this.ResumeLayout(false);
}
#endregion
/**
* The main entry point for the application.
*/
/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}
private void button1_Click (Object sender, System.EventArgs e)
{
System.Text.StringBuilder _sql = new System.Text.StringBuilder( );
_sql.Append( "INSERT INTO table " );
System.Text.StringBuilder _stat1 = new System.Text.StringBuilder();
if ( !this.textBox1.get_Text().Equals( "" ) ) _stat1.Append( "Entity1" );
if ( !this.textBox2.get_Text().Equals( "" ) )
{
if ( _stat1.get_Length() > 0 ) _stat1.Append( ", Entity2" );
else _stat1.Append( "Entity2" );
}
if ( !this.textBox3.get_Text().Equals( "" ) )
{
if ( _stat1.get_Length() > 0 ) _stat1.Append( ", Entity3" );
else _stat1.Append( "Entity3" );
}
if ( !this.textBox4.get_Text().Equals( "" ) )
{
if ( _stat1.get_Length() > 0 ) _stat1.Append( ", Entity4" );
else _stat1.Append( "Entity4" );
}
if ( !this.textBox5.get_Text().Equals( "" ) )
{
if ( _stat1.get_Length() > 0 ) _stat1.Append( ", Entity5" );
else _stat1.Append( "Entity5" );
}
System.Text.StringBuilder _stat2 = new System.Text.StringBuilder();
if ( !this.textBox1.get_Text().Equals( "" ) )
{
if ( _stat2.get_Length() > 0 ) _stat2.Append( ", " +
this.textBox1.get_Text() );
else _stat2.Append( this.textBox1.get_Text() );
}
if ( !this.textBox2.get_Text().Equals( "" ) )
{
if ( _stat2.get_Length() > 0 ) _stat2.Append( ", " +
this.textBox2.get_Text() );
else _stat2.Append( this.textBox2.get_Text() );
}
if ( !this.textBox3.get_Text().Equals( "" ) )
{
if ( _stat2.get_Length() > 0 ) _stat2.Append( ", " +
this.textBox3.get_Text() );
else _stat2.Append( this.textBox3.get_Text() );
}
if ( !this.textBox4.get_Text().Equals( "" ) )
{
if ( _stat2.get_Length() > 0 ) _stat2.Append( ", " +
this.textBox4.get_Text() );
else _stat2.Append( this.textBox4.get_Text() );
}
if ( !this.textBox5.get_Text().Equals( "" ) )
{
if ( _stat2.get_Length() > 0 ) _stat2.Append( ", " +
this.textBox5.get_Text() );
else _stat2.Append( this.textBox5.get_Text() );
}
if ( _stat1.get_Length() == 0 ) System.Windows.Forms.MessageBox.Show( "No
SQL statements can be made" );
else
{
_sql.Append( "(" +_stat1.ToString() + ")" );
_sql.Append( " VALUES " );
_sql.Append( "(" + _stat2.ToString() + ")" );
System.Windows.Forms.MessageBox.Show( _sql.ToString() );
}
}
}
Regards,
Lars-Inge Tønnessen