-
July 5th, 2009, 10:03 AM
#1
Problem with MS Access database...
Hey peaople... I have a problem with MS Access database.
I use C#.NET and MS Access 2002-2003 database...
Here is my code
Code:
...
using System.Data.OleDb;
namespace Потсетник
{
public partial class Form1 : Form
{
public OleDbConnection database;
DataGridViewButtonColumn editButton;
DataGridViewButtonColumn deleteButton;
public Form1()
{
InitializeComponent();
// Povrzhuvanje so databazata... #####################
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=zapis.mdb";
try
{
database = new OleDbConnection(connectionString);
database.Open();
//SQL query to list movies
string queryString = "SELECT ID, Name, LastName, Nick, MobileNumber, MobileNumber2, e-Mail, e-Mail2, Type FROM Table1 WHERE Table1.ID";
loadDataGrid(queryString);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return;
}
}
...
And now, when I start the application I get message „No value given for one or more required parameters“.
The mistake should be in the following queryString.
What do I do wrong ?
Thanks...
-
July 5th, 2009, 10:29 AM
#2
Re: Problem with MS Access database...
Welcome to the forum
Your query is incorrect. You are giving a Where condition but you are not providing any value for the ID column. You should correct your query, something like this would return all records
Code:
string queryString = "SELECT ID, Name, LastName, Nick, MobileNumber, MobileNumber2, e-Mail, e-Mail2, Type FROM Table1";
-
July 5th, 2009, 11:35 AM
#3
Re: Problem with MS Access database...
Originally Posted by Shuja Ali
Welcome to the forum
Thanks
Originally Posted by Shuja Ali
Your query is incorrect. You are giving a Where condition but you are not providing any value for the ID column. You should correct your query, something like this would return all records
Code:
string queryString = "SELECT ID, Name, LastName, Nick, MobileNumber, MobileNumber2, e-Mail, e-Mail2, Type FROM Table1";
Same message error (No value given for one or more required parameters)...
I have empty DataGridView... and when I start the aplication only the DataGridView to fullfill with the suitable base of...
Code:
// Code for load dataGridView...
string queryString = "SELECT ID, Name, LastName, Nick, MobileNumber, MobileNumber2, e-Mail, e-Mail2, Type FROM Table1";
loadDataGrid(queryString);
...
// Code for dataGridView properties...
public void loadDataGrid(string sqlQueryString)
{
OleDbCommand SQLQuery = new OleDbCommand();
DataTable data = null;
dataGridView1.DataSource = null;
SQLQuery.Connection = null;
OleDbDataAdapter dataAdapter = null;
dataGridView1.Columns.Clear();
//---------------------------------
SQLQuery.CommandText = sqlQueryString;
SQLQuery.Connection = database;
data = new DataTable();
dataAdapter = new OleDbDataAdapter(SQLQuery);
dataAdapter.Fill(data);
dataGridView1.DataSource = data;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ReadOnly = true;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Width = 90;
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[2].Width = 110;
dataGridView1.Columns[2].HeaderText = "Last Name";
dataGridView1.Columns[3].Width = 60;
dataGridView1.Columns[3].HeaderText = "Nick";
dataGridView1.Columns[4].Width = 80;
dataGridView1.Columns[4].HeaderText = "Mobile Number";
dataGridView1.Columns[5].Width = 80;
dataGridView1.Columns[5].HeaderText = "Mobile Number2";
dataGridView1.Columns[6].Width = 90;
dataGridView1.Columns[6].HeaderText = "e-Mail";
dataGridView1.Columns[7].Width = 90;
dataGridView1.Columns[7].HeaderText = "e-Mail2";
// insert edit button into datagridview
editButton = new DataGridViewButtonColumn();
editButton.HeaderText = "Edit";
editButton.Text = "Edit";
editButton.UseColumnTextForButtonValue = true;
editButton.Width = 80;
dataGridView1.Columns.Add(editButton);
// insert delete button to datagridview
deleteButton = new DataGridViewButtonColumn();
deleteButton.HeaderText = "Delete";
deleteButton.Text = "Delete";
deleteButton.UseColumnTextForButtonValue = true;
deleteButton.Width = 80;
dataGridView1.Columns.Add(deleteButton);
}
I don't know where I wrong... :/
-
July 5th, 2009, 01:02 PM
#4
Re: Problem with MS Access database...
I did not realize that you have a special character in the fieldnames. It is a bad practice to put special characters in the Column Names. You have a Hyphen in e-Mail & e-Mail2 fields. Either you rename them in your database or use a query like this
Code:
SELECT ID, Name, LastName, Nick, MobileNumber, MobileNumber2, [e-Mail], [e-Mail2], Type FROM Table1;
I have put square brackets for the email field names.
I would rather go and rename the fields.
Edit
One more thing. when you encounter these type of errors that are related to the SQL Query you wrote, the easiest way to debug them would be to copy the query and run it in the database directly that you are using. In this case, I would copy the query and open the access database, create a new query go to SQL view, paste the query and run it to see what the exact problem is.
Last edited by Shuja Ali; July 5th, 2009 at 01:06 PM.
Reason: Added a tip
-
July 5th, 2009, 04:50 PM
#5
Re: Problem with MS Access database...
Thanks man for the help.
I solved the problem with two tables...
Code:
...Type FROM Table1,Table2 WHERE Table2.typeID = Table1.typeID";
-
July 6th, 2009, 01:07 PM
#6
Re: Problem with MS Access database...
Originally Posted by SkyDriver
Thanks man for the help.
I solved the problem with two tables...
You are welcome. But I seriously don't understand how you solved the problem with two tables. Can you post the exact solution so that others will also get benefited.
-
July 7th, 2009, 08:18 AM
#7
Re: Problem with MS Access database...
Originally Posted by Shuja Ali
You are welcome. But I seriously don't understand how you solved the problem with two tables. Can you post the exact solution so that others will also get benefited.
In the first table are the information for the my contacts and in the second table are the group of the contacts (Friends, Coworkers, Relatives...).
So, my DataGridView is | Name | Last Name | Mobile | Mobile2 | e-Mail | e-Mail2 | Group |
Here is my code:
Code:
string queryString = "SELECT ID, Name, LastName, MobileNumber, MobileNumber2, eMeil, eMail2, Type FROM Table1,Table2 WHERE Table2.typeID = Table1.typeID";
loadDataGrid(queryString);
-
July 7th, 2009, 06:53 PM
#8
Re: Problem with MS Access database...
You may want to check out using the newer join syntax. Something like
Code:
SELECT ID
, Name
, LastName
, MobileNumber
, MobileNumber2
, eMeil
, eMail2
, Type
FROM Table1
INNER JOIN Table2
ON Table2.typeID = Table1.typeID
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|