Hi guys, sorry about bothering you with my newbie questions again, but i cant find why this thing isnt working, and also i need some advice if what i m doing is correct or not.

i m getting this error when trying to execute a Insert Command on my database:

System.NullReferenceException: Object reference not set to an instance of an object.
   at FirebirdSql.Data.Firebird.FbDataAdapter.Update (DataRow[] dataRows, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.UpdateFromDataTable(DataTable dataTable, DataTableMapping tableMapping)
   at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable)
   at Teste.cad_cli.Btn_salvarClick(Object sender, EventArgs e)

I ll post and explain my code, cause i m really new to C# and i m learning by myself just google for stuff.. so i think i may be messing everything up, here we go.

To get the ID to insert on the table i m using this:

        private void ClearFields() {
            //Código Autoincremento
            string sql = "SELECT GEN_ID(GEN_CLIENTES_ID,1) FROM RDB$DATABASE";
            FbCommand select = con.CreateCommand();
            select.CommandText = sql;
            FbDataReader reader = select.ExecuteReader();
            while(reader.Read()) {
                codigo = reader.GetInt32(0);
            }
            txtCli_cod.Text = codigo.ToString();
            txtCli_nome.Text = "";
            txtCli_sobrenome.Text = "";
        }


This gets the ID to insert on the table.. then i m using this to do the Insert:

        void Btn_salvarClick(object sender, System.EventArgs e)
        {
            DataTable dtTable = ds_cli.Tables[0];
            DataRow newRow = dtTable.NewRow();           
            newRow["CLI_COD"] = codigo;
            newRow["CLI_NOME"]   = txtCli_nome.Text;
            newRow["CLI_SOBRENOME"]  = txtCli_sobrenome.Text;
            dtTable.Rows.Add(newRow);
            try {
                da_cli.InsertCommand = con.CreateCommand();
                // DADOS:
                   da_cli.InsertCommand.CommandText = "INSERT INTO CLIENTES (CLI_COD,CLI_NOME, CLI_SOBRENOME) VALUES(@cli_cod,@cli_nome,@cli_sobrenome)";
                   da_cli.InsertCommand.Parameters.Add("@cli_cod", codigo);
                   da_cli.InsertCommand.Parameters.Add("@cli_nome", txtCli_nome.Text.ToString());
                   da_cli.InsertCommand.Parameters.Add("@cli_sobrenome", txtCli_sobrenome.Text.ToString());
                   da_cli.Update(ds_cli, "CLIENTES");
                   ds_cli.AcceptChanges();
            } catch(Exception ex) {                           
                txtPesq.Text = ex.ToString();
            }



The error occur on the bold part, if i use: INSERT...VALUES('" + txtCli_nome.Text.ToString() + "') it works, but if i use Add("@cli_nome", txtCli_nome.Text.ToString()), it gives me that error, anyone knows why am i getting that?

And also, am i doing the right thing here, or am i messing up and complicating everything?

Thanx for any help or opinion,

-Fábio

Reply via email to