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
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