Click here to Skip to main content
15,889,813 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
Hello Guys, am facing a little issue i got an error no value given for one or more required parameters.

What I have tried:

DataSet dsa = new DataSet();
            DataTable dt = new DataTable();
            dsa.Tables.Add(dt);
            OleDbDataAdapter da = new OleDbDataAdapter();
            da = new OleDbDataAdapter("SELECT [Flavours],COUNT(Flavours)As[asdf] From [Total] Where [Dates] >= #" + dateTimePicker1.Value.ToString("dd/MM/yyyy") + "# AND [Dates] <= #" + dateTimePicker2.Value.ToString("dd/MM/yyyy") + "# Group By [Flavours]", VCON);
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            VCON.Close();
Posted
Updated 27-Sep-16 1:17am
Comments
F-ES Sitecore 27-Sep-16 5:52am    
If this is an Access database you can get this issue if you're referring to something that doesn't exist, like a fieldname or table (either it doesn't exist or maybe you have misspelled it). It assumes the non-existent entity you are referring to is a parameter so expects you to supply it.

Start with a more basic query and add to it until you hit the issue, so start with just

select * from [Total]

then

select [Flavours] from [Total]

and so on.

Stop using string concatenation to pass values - use a parameterised query instead:
C#
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Flavours],COUNT(Flavours)As[asdf] From [Total] Where [Dates] BETWEEN ? AND  ? Group By [Flavours]", VCON);
da.SelectCommand.Parameters.AddWithValue("?", dateTimePicker1.Value);
da.SelectCommand.Parameters.AddWithValue("?", dateTimePicker2.Value);
da.Fill(dt);

If you are habitually using string concatenation, then be aware that your database is at risk for SQL Injection, where a user can destroy it just by typing in text boxes: xkcd: Exploits of a Mom[^]
Always use parameterised queries.
 
Share this answer
 
Comments
Member 9983063 27-Sep-16 5:26am    
sir am facing still that issue after using your query
OriginalGriff 27-Sep-16 5:48am    
Use the debugger and double check that it is exactly that code: single step it and make sure that the exception happens when you do the Fill operation.
Hi,
Your string containing query is not correct. Try pass values through parameters.
 
Share this answer
 
Comments
Member 9983063 27-Sep-16 5:31am    
am using this query but i have still get an error like that no value given for one or more required parameters.
Member 9983063 27-Sep-16 5:31am    
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Flavours],COUNT(Flavours)As[asdf] From [Total] Where [Dates] BETWEEN ? AND ? Group By [Flavours]", VCON);
da.SelectCommand.Parameters.AddWithValue("?", dateTimePicker1.Value);
da.SelectCommand.Parameters.AddWithValue("?", dateTimePicker2.Value);
da.Fill(dt);
OleDbDataAdapter da = new OleDbDataAdapter();
da = new OleDbDataAdapter("SELECT [Flavours],COUNT(Flavours)As[asdf] From [Total] Where [Dates] BETWEEN Columnname1 AND Columnname2 Group By [Flavours]", VCON);
da.SelectCommand.Parameters.AddWithValue("@Columnname1", dateTimePicker1.Value);
da.SelectCommand.Parameters.AddWithValue("@Columnname1", dateTimePicker2.Value);
da.Fill(dt);

Where @Columnname / @Parametername is same as in database.
also you can convert dateTimePicker1.Value to string or datetime as required in database side
 
Share this answer
 
v3
Comments
Patrice T 27-Sep-16 11:41am    
Do not use multiple solutions just to show some code.
Use Improve solution to update your solution.

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