Problem with MS Access database...
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Problem with MS Access database...

  1. #1
    Join Date
    Jul 2009
    Location
    Macedonia
    Posts
    4

    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...

  2. #2
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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";

  3. #3
    Join Date
    Jul 2009
    Location
    Macedonia
    Posts
    4

    Re: Problem with MS Access database...

    Quote Originally Posted by Shuja Ali View Post
    Welcome to the forum
    Thanks

    Quote Originally Posted by Shuja Ali View Post
    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... :/

  4. #4
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    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 02:06 PM. Reason: Added a tip

  5. #5
    Join Date
    Jul 2009
    Location
    Macedonia
    Posts
    4

    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";

  6. #6
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: Problem with MS Access database...

    Quote Originally Posted by SkyDriver View Post
    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.

  7. #7
    Join Date
    Jul 2009
    Location
    Macedonia
    Posts
    4

    Re: Problem with MS Access database...

    Quote Originally Posted by Shuja Ali View Post
    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);

  8. #8
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,440

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center