Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
use c# and Msaccess i just add value like this Mc access column data type (text,numeric-double,text,numeric-percentage)
but i insert this values(john,"","","")this one value value not inserted
this error
because i have only name listdata
help me

i need insert null values or submit buttonclick create new empty row

A first chance exception of type 'System.NullReferenceException' occurred in biodata.exe

Additional information: Object reference not set to an instance of an object.

What I have tried:

  private void button1_Click(object sender, EventArgs e)
    {
  
          con.Open();
           cmd.CommandText = "insert into hhh (name,answerinfloat,subject,avg)VALUES('" + a.Text + "','" + b.Text + "','" + c.Text + "','" + d.Text + "')";
       
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("name", a.Text);
            cmd.Parameters.AddWithValue("answerinfloat", b.Text);

            cmd.Parameters.AddWithValue("subject", c.Text);
            cmd.Parameters.AddWithValue("avg", d.Text);

    
          
            int n = cmd.ExecuteNonQuery();


            if (n > 0)
            {
                MessageBox.Show("Record Submitted", "Congrats");
            }
            else
                MessageBox.Show("insertion failed");
        
       con.Close();
}
Posted
Updated 30-Jun-22 11:33am

For starters, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The chances are that fixing that - throughout the whole of your code - will get rid of your other problem as well...
 
Share this answer
 
Try VALUES ('john',NULL,NULL,NULL). This uses the NULL keyword. I have used this for numbers, text and Date/Time fields.
 
Share this answer
 
Comments
CHill60 1-Jul-22 5:21am    
Far better is
INSERT INTO hhh (name) VALUES ('john');
- that way if any defaults are set on any of the NULL columns then they will be applied.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900