Click here to Skip to main content
15,910,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi

I saved some pictures in the database ! but now i want to retrive some of it.

according to the passage when i select any row of datagrid the picturebox must show picture that have be relationship with that. for this action i used this code but it's error: There is no row at possition 0. how can i resolved this problems.

C#
string Username=dataGridView1.CurrentRow.Cells["Username"].Value.ToString();
string strSql="Select Image From tbUsers Where Username='"+Username+"'";
if (Conn.State != ConnectionState.Open)
{
    SqlDataAdapter Da = new SqlDataAdapter(strSql, Conn);
    DataSet DS = new DataSet();
    Conn.Open();
    Da.Fill(DS, "tbUsers");
    Conn.Close();
   try
    {
        byte[] ArrPic = (byte[])(DS.Tables["tbUsers"].Rows[0]["Image"]);
        MemoryStream MEM = new MemoryStream(ArrPic);
        pictureBox1.Image = Image.FromStream(MEM);
    }
    catch (Exception EX)
    { }

}

Conn is sqlconnection
Da is dataAdapter
Posted

1 solution

The error message clearly says that there is no data present at Row[0]. Please run this query in the database and check if any data is present.
SQL
Select Image From tbUsers Where Username='yourusername'

If you fetching the UserName value from the datagrid, make sure no extra characters are being appended or space is being appended. Check the exact UserName that is getting passed in the query.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900