Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
cmd.ExecuteNonQuery();

this line not execute
showing error -
No value given for one or more required parameters


What I have tried:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        public OleDbConnection con;
        public OleDbCommand cmd;

    

        protected void save_Click(object sender, EventArgs e)
        {
            con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\demo.accdb");
            con.Open();       
            string sql="insert into data1 values("+ txtname.Text+")";
            cmd = new OleDbCommand(sql,con);
            cmd.ExecuteNonQuery();
            con.Close();



        }

    }
}
Posted
Updated 4-Feb-18 2:42am

1 solution

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?

Then, look at your command: it's a bad idea to not specify the column names to insert into, because unless your table has only a single column, SQL will try to insert your values starting from the first column (which is normally an ID column, often IDENTITY which means you can't write to it anyway).
C#
INSERT INTO MyTable MyColumn1, MyColumn2 VALUES (@ValueForColumn1, @ValueForColumn2)
From your error message, you have multiple columns and they cannot be NULL so SQL expects a value to be provided.
 
Share this answer
 
Comments
manish7664 4-Feb-18 9:20am    
its working in parameter query ..
thnx OriginalGriff..
OriginalGriff 4-Feb-18 9:30am    
You're welcome!
Don't forget to check the rest of your app - if you miss one concatenation, your DB is at risk.

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