从数据库中检索每个选择的数据网格的图像 [英] Retrive image from database for every choise of datagrid

查看:79
本文介绍了从数据库中检索每个选择的数据网格的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在数据库中保存了一些图片!但现在我想取一些.

根据段落,当我选择datagrid的任何行时,picturebox必须显示与此相关的图片.为此,我使用了这段代码,但是它的错误是:位置0处没有行.我该如何解决此问题.

 字符串用户名= dataGridView1.CurrentRow.Cells [" 用户名"].Value.ToString();
字符串 strSql = "  +用户名+  '";
如果(连接状态!= ConnectionState.Open)
{
    SqlDataAdapter Da =  SqlDataAdapter(strSql,Conn);
    DataSet DS =  DataSet();
    Conn.Open();
    Da.Fill(DS," );
    Conn.Close();
   尝试
    {
        字节 [] ArrPic =(字节 [])(DS.Tables [  tbUsers"].行["  图片"]);
        MemoryStream MEM =  MemoryStream(ArrPic);
        pictureBox1.Image = Image.FromStream(MEM);
    }
    捕获(例外EX)
    {}

}


Conn是sqlconnection
Da是dataAdapter

解决方案

错误消息明确指出在Row [0]处没有数据.请在数据库中运行此查询,并检查是否存在任何数据.

 选择 图片 来自 tbUsers 位置用户名= ' 您的用户名' span> 


如果您从数据网格中获取UserName值,请确保没有附加任何字符或附加空格.检查在查询中传递的确切用户名.


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.

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

解决方案

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.

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.


这篇关于从数据库中检索每个选择的数据网格的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆