object reference not set to an instance of an object when using parameter

Can someone tell me how i can set the object reference to an instance? .... Here, user_id is the parameter which takes a textbox value into the sql statement.

private void button1_Click(object sender, EventArgs e)
{

    OracleConnection con = new OracleConnection("Data Source=KBETEST; Persist Security Info=TRUE; User ID=dbo; Password=dbo123; Unicode=True");
    DataSet ds = new DataSet();
    OracleDataAdapter adap = new OracleDataAdapter();
    OracleCommandBuilder b = new OracleCommandBuilder(adap);

    adap = new OracleDataAdapter("insert into banks_ben_branch_99 (ben_bank_id, ben_brn_code, brn_name,ben_brn_addr1, ben_brn_loc, ben_brn_state, ben_brn_city, ben_bank_city, coun_code,brn_stat, remarks, brn_id, user_id, pc_tcp_ip, rtgs_stat, pay_brn_code,sys_date) select bankid,benbrn_code,brn_name,substr(brn_addr,1,100),brn_loc, brn_stat, brn_city, brn_city, coun_code,'A', remarks, '15', :user_id,'172.20.1.109', rtgs_stat, benbrn_code,sysdate from bbbt",con);
    adap.InsertCommand.Parameters.Add("user_id", OracleType.VarChar,20, "user_id").Value = textBox1.Text;
    adap.Fill(ds,"A");
    DataTable table = ds.Tables["A"];
    dataGridView1.DataSource = ds.Tables["A"];
    con.Dispose();
}

thanks!

edit

private void button1_Click(object sender, EventArgs e)
    {

       OracleConnection con = new OracleConnection("Data Source=KBETEST; Persist Security Info=TRUE; User ID=dbo; Password=dbo123; Unicode=True");
       DataSet ds = new DataSet();
       OracleDataAdapter adap = new OracleDataAdapter();
       OracleCommandBuilder b = new OracleCommandBuilder(adap);
       string str = "insert into banks_ben_branch_99 (ben_bank_id, ben_brn_code, brn_name,ben_brn_addr1, ben_brn_loc, ben_brn_state, ben_brn_city, ben_bank_city, coun_code,brn_stat, remarks, brn_id, user_id, pc_tcp_ip, rtgs_stat, pay_brn_code,sys_date) select bankid,benbrn_code,brn_name,substr(brn_addr,1,100),brn_loc, brn_stat, brn_city, brn_city, coun_code,'A', remarks, '15', :user_id, '172.20.1.109', rtgs_stat, benbrn_code,sysdate from bbbt";
       con.Open();
       adap.InsertCommand = new OracleCommand(str, con);
       adap.InsertCommand.Parameters.Add("user_id", OracleType.VarChar,20).Value = textBox1.Text;
       adap.InsertCommand.ExecuteNonQuery();

       con.Dispose();
        }

thanks everyone for your help!! i got it!

Jon Skeet
people
quotationmark

EDIT: There are multiple things wrong with your code:

  • You're not specifying the user_id parameter in the command, although it's in the SQL
  • You're trying to use the InsertCommand of the adapter even though you haven't specified any insertion SQL
  • You're trying to fill a dataset, but you haven't specified a query - just an insert command.

I suspect you shouldn't be using a data adapter at all. If you just need to insert some data, use:

using (var connection = new OracleConnection(...)
{
    connection.Open();
    string sql = "insert into banks_ben_branch_99 [... as before ...]";
    using (var command = new OracleCommand(sql, conn))
    {
        command.Parameters.Add("user_id", OracleType.VarChar, 20)
                          .Value = textBox1.Text;
        command.ExecuteNonQuery();
    }
}

people

See more on this question at Stackoverflow