0

I have a problem to get specify value from my query. I receive one row for my GridView but when i try to read that one value to get it into some variable it said me that there is no any value.

else if (Request.QueryString.AllKeys.Contains("a"))
{
    String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = ?";
    connection.OPEN();

    OleDbCommand sql_s = new OleDbCommand(sql_user, polaczenie);

    sql_s.Parameters.add("?", OleDbType.Integer).Value = Request.QueryString["a"];

    OleDbDataAdapter sqlDa = new OleDbDataAdapter(sql_s);

    DataTable dt = new DataTable();
    sqlDa.Fill(dt);

    GRID_VIEW1.DataSource = dt;

    GRID_VIEW1.DataBind();

    OleDbDataReader customer = sql_s.ExecuteReader();

    // i want to get SHORT_NAME so that one below i think dosent work

    // but any way it make me an Exception of any value not found but on GridView it shows me clearly one ROW
    learerLabel.Text = (String) cmd.ExecuteScalar();

    connection.Close();
}

I do it in many ways like some Session or Reader but i dont remmeber my all tries now, but always it said me that there is no value in my query. What am I doing wrong? Thanks

Adriano
  • 874
  • 2
  • 11
  • 37

2 Answers2

0

Try this, replace the parameter name to @ID, refer the link below for the msdn documentation on passing Parameter

SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @ID
sql_s.Parameters.add("@ID", OleDbType.Integer).Value = Request.QueryString["a"];

More details on passing parameter is available in the below msdn https://msdn.microsoft.com/en-us/library/bbw6zyha(v=vs.110).aspx

BipinR
  • 473
  • 7
  • 19
  • Thanks, i will try it. But could you tell me what is the best way to Execute that value from this query (row)? – Adriano Mar 09 '17 at 20:00
  • I'm not sure if I understood your question properly, but you can use either data reader or the data adapter. If your query is returning only a few rows you can use data reader otherwise I would recommend using data adapter. – BipinR Mar 09 '17 at 21:07
0

Not better is do a Class like:

    public class MyClass
{
    public int Id { get; set; }
    public string ShortName { get; set; }
    public string Name { get; set; }
    public int Regon { get; set; }
}

Next to get data from your query try this:

            CustomClass item = new CustomClass();

        String sql_user = "SELECT c1.ID, c1.SHORT_NAME, ao1.NAME, c1.REGON FROM (tabel1 c1 LEFT JOIN tabel2 ao1 ON c1.ID = ao1.ID WHERE c1.ID = @id";

        using (var conn = new OleDbConnection("connectionString"))
        {
            conn.Open();
            using (var cmd = new OleDbCommand(sql_user, conn))
            {
                cmd.Parameters.Add("@id", OleDbType.Integer).Value = Request.QueryString["a"];
                cmd.Prepare();

                var reader = cmd.ExecuteReader();

                if(reader.Read())
                {
                    item.Id = (int)reader["ID"];
                    item.ShortName = reader["SHORT_NAME"].ToString();
                    item.Name= reader["NAME"].ToString();
                    item.Regon = (int)reader["REGON"];
                }
                conn.Close();
            }

        }

Edit: If you dont want use it, you need change taht line:

OleDbDataReader customer = sql_s.ExecuteReader();

to:

            OleDbDataReader customer;
        var dr = sql_s.ExecuteReader();

        if (dr.Read())
            customer = dr;

Try this and plis write it work

Newer
  • 79
  • 1
  • 11