将选定的图像从gridview插入到ASP.NET中 [英] Insert selected image from gridview to in ASP.NET

查看:117
本文介绍了将选定的图像从gridview插入到ASP.NET中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将选定的图像从gridview插入到mysql表中.当我尝试时,它会给我消息
App_Web_zptg3ta3.dll中出现类型为"System.InvalidCastException"的异常",但未在用户代码中处理.
附加信息:无法将类型为"System.Int32"的对象转换为类型为"System.Data.DataRow"."
任何示例帮助将不胜感激.
以下是我的代码:

I want to insert selected image from gridview into mysql table. When I try to, it gives me message saying
"An exception of type ''System.InvalidCastException'' occurred in App_Web_zptg3ta3.dll but was not handled in user code.
Additional information: Unable to cast object of type ''System.Int32'' to type ''System.Data.DataRow''."
Any example help would be appreciated.
Below is my code:

if (gvImages.SelectedIndex != -1)
        {
            //this is the predicted problem line
            DataRow SelectedRowValue = ((DataRow)gvImages.SelectedValue);
            byte[] ImageBytes = (byte[])SelectedRowValue.ItemArray[1];
            MySqlCommand cmd2 = new MySqlCommand("INSERT INTO rasmlar (Rasm)VALUES (@ImageSource)", con);
            cmd2.Parameters.Add("@ImageSource", MySqlDbType.Blob, ImageBytes.Length).Value = ImageBytes;
            cmd2.ExecuteNonQuery();
        }



我尝试过的事情:

GridViewRow行=(GridViewRow)gvImages.SelectedRow;
DataRowView SelectedRowValue =((DataRowView)gvImages.SelectedValue);



What I have tried:

GridViewRow row = (GridViewRow)gvImages.SelectedRow;
DataRowView SelectedRowValue = ((DataRowView)gvImages.SelectedValue);

推荐答案

该错误几乎是不言自明的.这意味着您不能将SelectedValue强制转换为DataRow,因为SelectedValue将返回一个整数.另外,如果要获取SelectedRow,则可能要在SelectedIndexChanged事件中进行处理.

但是请记住,为了触发SelectedIndexChanged事件,您需要在GridView中设置ShowSelectButton CommandField或将AutoGenerateSelectButton设置为true.例如:

The error is pretty much self-explanatory. This means that you cannot cast the SelectedValue to a DataRow because the SelectedValue will return an integer. Also If you want to get the SelectedRow, you might want to handle it at SelectedIndexChanged event.

But keep in mind that in order to fire the SelectedIndexChanged event, you need to setup the ShowSelectButton CommandField or set the AutoGenerateSelectButton to true in your GridView. For example:

<asp:gridview id="GridView1" runat="server"

	AutoGenerateColumns="False" 

	OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
	<columns>
		<asp:commandfield showselectbutton="True" />
		<asp:boundfield />
		<asp:boundfield />
	</columns>
</asp:gridview>



然后,您可以像这样引用Image控件:



Then you can reference the Image control like this:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
       Image img  = (Image)GridView1.SelectedRow.FindControl("TheImageID");
       if(img != null){
            //do stuff
       }
} 



但是由于您要将图像另存为blob,所以我认为您现有的代码不起作用.您将需要先上传图像,然后流传输上传的图像以获得字节数组.有大量的例子可以证明这一点,您只需要在google上找到它们即可.

这是其中之一:
保存并检索BLOB来自ASP.Net,C#和VB.Net中MySql数据库的图像 [



But since you want to save the image as blob, then I don''t think your existing code will work. You would need to upload the image first and then stream the uploaded image to get the byte array. There are tons of examples that demonstrate about that, you just need to find them at google.

This one is one of them:
Save and Retrieve BLOB Images from MySql Database in ASP.Net, C# and VB.Net[^]


这篇关于将选定的图像从gridview插入到ASP.NET中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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