0
votes

'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;
        }
    }
}
2
Where is the error being thrown? And what is the question?Nahuel Ianni
Having a static connection is a very bad ideaSLaks
Are you missing a using directive or an assembly reference? Have you checked or done any research?Daniel Kelley
Where do you see a 'Rows' property here : msdn.microsoft.com/fr-fr/library/system.object(v=vs.110).aspx ?ArthurCPPCLI

2 Answers

2
votes

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

1
votes

Change your getdata() function signature to

private DataTable GetData(string query){

}