0

'Object' does not contain a definition for 'Rows' and no extension method 'Rows' accepting a first argument of type 'object' could be found are you missing a using directive or an assembly reference?

The code is given below:

public partial class Default3 : System.Web.UI.Page
{
    static SqlConnection con = new SqlConnection(@"connectionString");
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!this.IsPostBack)
        {
            ddlImg.DataSource = GetData("SELECT EmpID, EmpName FROM Tbl_Emp");
            ddlImg.DataTextField = "EmpName";
            ddlImg.DataValueField = "EmpID";
            ddlImg.DataBind();
        }

    }

    private object GetData(string query)
    {
        DataTable dt = new DataTable();
        string constr = ConfigurationManager.ConnectionStrings["tbFiles2ConnectionString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand(query))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    sda.Fill(dt);
                }
            }
            return dt;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlCommand cmd = new SqlCommand();
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = ("INSERT INTO Tbl_Emp(EmpID, EmpName, EmpPic ) VALUES('"+TextBox1.Text+"','"+TextBox2.Text+"','" + FileUpload1 + "')");
        cmd.ExecuteNonQuery();
        con.Close();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        string id = ddlImg.SelectedItem.Value;
        Image1.Visible = id != "0";
        if (id != "0")
        {
            byte[] bytes = (byte[])GetData("SELECT Data FROM Tbl_Emp WHERE Emp_ID =" + id).Rows[0]["Data"]; // Error popup here
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;
        }
    }
}
Nahuel Ianni
  • 3,177
  • 4
  • 23
  • 30
Vipin
  • 261
  • 6
  • 20
  • 44

2 Answers2

2

The problem is that GetData returns object which has no Rows property. For that reason you get the compiler error at:

...GetData("SELECT Data FROM Tbl_Emp WHERE Emp_ID =" + id).Rows[0]...

Return a DataTable instead:

private DataTable GetData(string query)
{
    DataTable dt = new DataTable();
    string constr = ConfigurationManager.ConnectionStrings["tbFiles2ConnectionString"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                sda.Fill(dt);
            }
        }
    }
    return dt;
}

Note that you should

Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
1

Change your getdata() function signature to

private DataTable GetData(string query){

}
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Binson Eldhose
  • 749
  • 1
  • 6
  • 14