Devo dire che sono un po' tanto tarda, ma prima o poi sto asp.net lo imparerò! dunque...
Classe:
codice:
public static string insertRecord (string tableName, string nome, string TxtbStato)
{
OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand comm = new OleDbCommand("SELECT * FROM " + tableName +" WHERE " + nome +"=" + TxtbStato ,conn);
OleDbDataAdapter DA = new OleDbDataAdapter(comm);
DataTable MyTable = new DataTable();
DA.Fill(MyTable);
if (MyTable.Rows.Count==0)
{
comm.CommandText="insert into " + tableName + " ( " + nome + ") values ('" + TxtbStato + "')";
comm.ExecuteNonQuery();
conn.Close();
return "";
}
else
{
return "dato già presente";
}
}
Codice pag:
codice:
private void ButStato_Click(object sender, System.EventArgs e)
{
try
{
string tableName = "stati";
string nome = "stato";
Access.insertRecord(tableName, nome, txtbStato.Text);
Risposta.Text="Dati inseriti correttamente";
txtbStato.Text="";
}
catch
{
if (txtbStato.Text=="")
{
Risposta.Text="Devi completare il campo";
}
else
{
string tableName = "stati";
string nome = "stato";
string Ris = Risposta.Text=Access.insertRecord(tableName, nome, txtbStato.Text);
if (Ris=="")
{
Risposta.Text="Operazione NON eseguita";
}
else
{
Risposta.Text=Ris;
}
}
}
In fase di compilazione tutto ok... ma quando inserisco un dato mi da questo errore:
Nessun valore specificato per alcuni parametri necessari.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.OleDb.OleDbException: Nessun valore specificato per alcuni parametri necessari.
Source Error:
Line 38: OleDbDataAdapter DA = new OleDbDataAdapter(comm);
Line 39: DataTable MyTable = new DataTable();
Line 40: DA.Fill(MyTable);
Line 41: if (MyTable.Rows.Count==0)
Line 42: {
Source File: c:\inetpub\wwwroot\ppp\areariservata\access.cs Line: 40