Reputation: 27
I am writing a program in C# using Visual Studio 2010 and gets an error when retrieving data from a .sdf
file.
private void BasicSearch_button_Click(object sender, EventArgs e)
{
SqlCeConnection cn = new SqlCeConnection(@"Data Source=" + Path.Combine(Application.StartupPath, "FRAT_DB.sdf") + ";Password=P@ssw0rd;Persist Security Info=False;Max Database Size=256");
string query = @"SELECT Person.PersonID, Person.Name, PhotoTags.PhotoID, Photo.Path, Photo.Location, Person.Age, Person.P_Group, Person.Email, Photo.Date "+
"FROM Person INNER JOIN PhotoTags ON Person.PersonID = PhotoTags.PersonID INNER JOIN "+
"PhotoON PhotoTags.PhotoID = Photo.PhotoID "+"WHERE (Person.Name LIKE '%%')";
if (txtName1.Text.Trim().ToString() == "" || txtName2.Text.Trim().ToString() == "")
{ MessageBox.Show("Enter Both, Name1 and Name2"); return; }
else if (radioButtonAND.Checked)
{
query = @"select pt.PhotoID,Photo.Path,photo.Location, Photo.Date from Person p inner join PhotoTags pt on p.PersonID=pt.PersonID inner join Photo on pt.photoID=photo.photoID where Name like '%" + txtName1.Text + "%' and pt.Photoid in (select pt.PhotoID from Person p inner join PhotoTags pt on p.PersonID=pt.PersonID where Name like '%" + txtName2.Text + "%')";
}
else
{
query = @"SELECT DISTINCT Person.PersonID, Person.Name, PhotoTags.PhotoID, Photo.Path, Photo.Location, Person.Age, Photo.Date
FROM Person INNER JOIN PhotoTags ON Person.PersonID = PhotoTags.PersonID INNER JOIN
PhotoON PhotoTags.PhotoID = Photo.PhotoID
WHERE (Person.Name LIKE '%%')";
query += " AND (Person.Name like '%" + txtName1.Text + "%' OR Person.Name like '%" + txtName2.Text + "%')";
}
if (cn.State == ConnectionState.Closed) cn.Open();
SqlCeCommand cm_Search = new SqlCeCommand(query, cn);
try
{
SqlCeDataReader rdr = cm_Search.ExecuteReader();
List<PersonPhoto> personPhoto = new List<PersonPhoto>();
List<PersonPhotoWithoutName> personPhotoWithoutName = new List<PersonPhotoWithoutName>();
bool HasRows = rdr.Read();
if (HasRows)
{
while (rdr.Read())
{
if (radioButtonAND.Checked)
{
personPhotoWithoutName.Add(new PersonPhotoWithoutName
{
PhotoID = Convert.ToInt32(rdr["PhotoID"].ToString()),
Location = rdr["location"].ToString(),
Date = rdr["Date"] != null ? Convert.ToDateTime(rdr["Date"]) : DateTime.Now,
path = rdr["path"].ToString(),
});
}
else
{
personPhoto.Add(new PersonPhoto
{
Name = rdr["Name"].ToString(),
Location = rdr["location"].ToString(),
Date = rdr["Date"] != null ? Convert.ToDateTime(rdr["Date"]) : DateTime.Now,
path = rdr["path"].ToString()
});
}
}
rdr.Close();
}
else
{ MessageBox.Show("No Records Found"); selectedPictureBox.Image = null; rdr.Close(); }
if (personPhoto.Count > personPhotoWithoutName.Count)
{
DataGridPersons.DataSource = personPhoto;
DataGridPersons.Refresh();
}
else
{
DataGridPersons.DataSource = personPhotoWithoutName;
DataGridPersons.Refresh();
}
}
catch (Exception exp)
{
throw exp;
}
if (cn.State != ConnectionState.Closed) cn.Close();
}
Whenever I try to search for someone this exception error comes up:
There was an error parsing the query. [ Token line number = 3,Token line offset = 18,Token in error = . ]
Any help?
thanks in advance!
Upvotes: 0
Views: 366
Reputation: 1503180
I suspect this is the problem:
... INNER JOIN
PhotoON PhotoTags.PhotoID = Photo.PhotoID ...
I suspect you meant:
... INNER JOIN
Photo ON PhotoTags.PhotoID = Photo.PhotoID ...
Note the space between Photo
and ON
. I suspect you could reformat your query so this would be rather clearer, to be honest...
Upvotes: 4