0
votes

I use this query string :

string queryString = "SELECT email_uzivatele from Reklama where email_uzivatele like '%" + @email_uzivatele + "%'";

And test it in console application.

When string is like this :

string queryString = "SELECT * from Reklama where email_uzivatele like '%" + @email_uzivatele + "%'";

it works fine. But when I select specific column like this:

string queryString = "SELECT email_uzivatele from Reklama where email_uzivatele like '%" + @email_uzivatele + "%'";

it doesn't work and it is causing this error:

System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
at System.Int32.Parse(String s)
at Smart.Advertising.DatabaseLayer.AdvertDatabaseLayer.SelectByEmail(String email_uzivatele) in f:\BP\SmartAdvertising.GuiMvc\Smart.Advertising.DatabaseLayer\AdvertDatabaseLayer.cs:line 228

I don't understand why....

Please can you help me?

Edit: Here is more code from my console aplication:

namespace ConsoleApplication1
{
    class Program
    {
        public static void Main(string[] args)
        {
            AdvertDatabaseLayer.Instance.SelectByEmail("jan");
        }
    }
}

And this line is causing error:

AdvertDatabaseLayer.Instance.SelectByEmail("jan");

This is database layer where i use query string :

public List<Advert> SelectByEmail(string email_uzivatele)
{
    string queryString = "SELECT email_uzivatele from Reklama where email_uzivatele like '%" + @email_uzivatele + "%'";
    SqlCommand command = new SqlCommand(queryString, Connection);

    command.Parameters.AddWithValue("@email_uzivatele", "");

    try
    {
        SqlDataReader reader = command.ExecuteReader();
        List<Advert> advert = new List<Advert>();
        while (reader.Read())
        {
            Advert a = new Advert();
            a.id_reklamy = int.Parse(reader[0].ToString());
            a.nazev_reklamy = reader[1].ToString();
            a.typ_reklamy = reader[2].ToString();
            a.cena_reklamy = int.Parse(reader[3].ToString());
            a.email_uzivatele = reader[4].ToString();
            a.datumz = reader[5].ToString();
            a.datumk = reader[6].ToString();

            Console.WriteLine("email_uzivatele: " + " " + " " + a.email_uzivatele);

            advert.Add(a);
        }

        reader.Close();
        return advert;
    }
    catch (Exception ex)
    {
        chyba.zapsat_do_souboru(ex.Message);
        Console.OpenStandardOutput();
        Console.WriteLine(ex);
        //zalogovat chybu
        return null;
    }
}
1
Please post more of the code from you console application. This could be an issue of how you are reading the input. - BPS
Now i edited my post and added more code from console application. - Jakub Bocek
The added code seems to have no whatsoever relationship with your SQL query?!? - Uwe Keim
BTW: Little Bobby Tables alert! - Uwe Keim
a.id_reklamy = int.Parse(reader[0].ToString()); - it's trying to read data from the first column and convert it to an int. But there's only one column in this result set, and it doesn't contain an int. - Damien_The_Unbeliever

1 Answers

0
votes

I found solution the problem i was looping through all parametr but in query string i had only one parametr so the solution is this:

  while (reader.Read())
                {

                    Advert a = new Advert();
                    a.email_uzivatele = reader[0].ToString();



                    Console.WriteLine("email_uzivatele: " + " " + " " + a.email_uzivatele);

                    advert.Add(a);
                }