Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi , i am in needed in Helping very much .... from morning till now(night) i 'm working on c# login through access 2007 ... i explore alot google any websites u know but none of them doesnt reply ... please help meeee ..... i really got tired!
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication34
{
    public partial class Form1 : Form
    {
        public OleDbConnection con = new OleDbConnection();
        public Form1()
        {
            InitializeComponent();
            string strcon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\\Users\\Persian\\Desktop\\a.accdb ";
            con = new OleDbConnection(strcon);
            try
            {
                con.Open();
            }
            catch (OleDbException e)
            {
                MessageBox.Show("Access Error");
                MessageBox.Show("Error code= " + e.ErrorCode);
                MessageBox.Show("Error Message=" + e.Message);

            }
            catch (InvalidOperationException e)
            {
                MessageBox.Show("Invalid message"+ e.Message);
            }
            if (con.State != ConnectionState.Open)
            {
                MessageBox.Show("Database connection is failed");
                Application.Exit();
            
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string cmdstring = "SELECT * FROM log";
            cmdstring += "WHERE (user=@p) AND (pass=@q)";
            OleDbDataAdapter logindataadapter = new OleDbDataAdapter();
            DataTable accdatatable = new DataTable();
            OleDbCommand acccommand = new OleDbCommand();
            Form1 a = new Form1();
            acccommand.Connection = con;
            acccommand.CommandType = CommandType.Text;
            acccommand.CommandText = cmdstring;
            acccommand.Parameters.Add("@p",OleDbType.Char).Value = textBox1.Text;
            acccommand.Parameters.Add("@q", OleDbType.Char).Value = textBox2.Text;
            logindataadapter.SelectCommand = acccommand;
            logindataadapter.Fill(accdatatable);
            if (accdatatable.Rows.Count > 0)
            {
                MessageBox.Show("Login susseful");
                a.Show();
                this.Hide();

            }
            else
                MessageBox.Show("No Matched username/password found");
            accdatatable.Dispose();
            acccommand.Dispose();
            logindataadapter.Dispose();
        }
    }
}


acctually it runs but when i enter right user and pass on this line
C#
logindataadapter.Fill(accdatatable);
is saying this error "syntax error in FROM clause" ... what is its problem friends

many thanks
Posted
Updated 11-Oct-13 6:17am
v2
Comments
Richard C Bishop 11-Oct-13 12:14pm    
Well, you wasted even more time by not even describing any problem or what sort errors you might be getting.
Lida Zareian 11-Oct-13 12:18pm    
i improve my problem
Ron Beyer 11-Oct-13 12:15pm    
I understand you are tired, but you need to explain more...

What do you expect it to do?
What is it actually doing?

Please use the Improve question (green link under the question) to add more information.
Lida Zareian 11-Oct-13 12:18pm    
thanx , i did . please check again and help me yaar.

1 solution

You need to add a space after "log" or before "WHERE".

When you are getting SQL syntax errors, it is wise to take that into SQL Server MS and run the query until it works. When moving it back to C#, make sure all spaces and punctuation persist.

Also, while debugging, if you looked at the concatenated string of your query, you would have noticed the lack of the space.
 
Share this answer
 
v3
Comments
Thomas Daniels 11-Oct-13 12:23pm    
My 5!
Richard C Bishop 11-Oct-13 12:24pm    
Thank you ProgramFOX!
Ron Beyer 11-Oct-13 12:27pm    
Nice catch. 5'd
Richard C Bishop 11-Oct-13 12:28pm    
Thank you Ron!
Lida Zareian 11-Oct-13 12:27pm    
thaaanx alot ... god bless u ... i wish u the best :)

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