I can not understand where the error, I have another method that works properly it works the same way. It could be the 'WHERE' statement the problem?
Method that generates the exception:
public string[] GetData(string name, string surname)
{
customerInformation = new List<string>();
Connection();
string sqlQuery = "SELELCT " +
"Nome, " +
"Cognome, " +
"Giorno_Nascita, " +
"Mese_Nascita, " +
"Anno_Nascita, " +
"Luogo_Nascita, " +
"Residenza, " +
"Provincia_Residenza, " +
"Indirizzo_Residenza, " +
"Civico_Residenza, " +
"Domicilio, " +
"Provincia_Domicilio, " +
"Indirizzo_Domicilio, " +
"Civico_Domicilio, " +
"Mail, " +
"Telefono_Fisso, " +
"Telefono_Mobile, " +
"Fax, " +
"Codice_Fiscale " +
"FROM DatiClienti WHERE Nome LIKE " + name + " AND Cognome LIKE " + surname + "'";
dbCommand = new SqlCommand(sqlQuery, dbConnection);
dbReader = dbCommand.ExecuteReader(); **//Exception Here**
if(dbReader.HasRows)
{
while(dbReader.Read())
{
customerInformation.Add(dbReader["Nome"].ToString());
customerInformation.Add(dbReader["Cognome"].ToString());
customerInformation.Add(dbReader["Giorno_Nascita"].ToString());
customerInformation.Add(dbReader["Mese_Nascita"].ToString());
customerInformation.Add(dbReader["Anno_Nascita"].ToString());
customerInformation.Add(dbReader["Luogo_Nascita"].ToString());
customerInformation.Add(dbReader["Residenza"].ToString());
customerInformation.Add(dbReader["Provincia_Residenza"].ToString());
customerInformation.Add(dbReader["Indirizzo_Residenza"].ToString());
customerInformation.Add(dbReader["Civico_Residenza"].ToString());
customerInformation.Add(dbReader["Domicilio"].ToString());
customerInformation.Add(dbReader["Provincia_Domicilio"].ToString());
customerInformation.Add(dbReader["Indirizzo_Domicilio"].ToString());
customerInformation.Add(dbReader["Civico_Domicilio"].ToString());
customerInformation.Add(dbReader["Mail"].ToString());
customerInformation.Add(dbReader["Telefono_Fisso"].ToString());
customerInformation.Add(dbReader["Telefono_Mobile"].ToString());
customerInformation.Add(dbReader["Fax"].ToString());
customerInformation.Add(dbReader["Codice_Fiscale"].ToString());
}
}
else
{
MessageBox.Show("Non ci sono dati per questo cliente.");
}
dbReader.Close();
Disconnect();
return customerInformation.ToArray();
}
And this is the method working properly:
public List<string> GetSearchableData()
{
Connection();
customerName = new List<string>();
string sqlQuery = "SELECT * FROM DatiClienti";
dbCommand = new SqlCommand(sqlQuery, dbConnection);
dbReader = dbCommand.ExecuteReader();
if(dbReader.HasRows)
{
while(dbReader.Read())
{
string name = dbReader.GetString(1);
string surname = dbReader.GetString(2);
customerName.Add(name + " " + surname);
}
}
return customerName;
}
How can I fix this?
Thanks in advance.