Click here to Skip to main content
15,889,863 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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();
        }


What I have tried:

GridViewRow row = (GridViewRow)gvImages.SelectedRow;
DataRowView SelectedRowValue = ((DataRowView)gvImages.SelectedValue);
Posted
Updated 4-Aug-16 6:00am
Comments
F-ES Sitecore 4-Aug-16 6:36am    
gvImages.SelectedValue will be an int. Given what you've posted it's impossible for us to know why, we don't know what gvImages is or how it is populated. You might be wanting to do something like this instead

GridViewRow SelectedRowValue = ((GridViewRow)gvImages.Rows[gvImages.SelectedValue]);

As I said though, we can't tell much from what you've posted.

1 solution

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.NET
<asp:gridview id="GridView1" runat="server"
	AutoGenerateColumns="False" 
	OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
	<columns>
		<asp:commandfield showselectbutton="True" />
		<asp:boundfield />
		<asp:boundfield />
	</columns>
</asp:gridview>


Then you can reference the Image control like this:

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


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[^]
 
Share this answer
 
v3
Comments
Karthik_Mahalingam 4-Aug-16 22:42pm    
5
Vincent Maverick Durano 5-Aug-16 5:32am    
Thank you, Karthik! :)
abdujalilc 5-Aug-16 6:15am    
Thanks, good explanation. If I want that selected image to be shown in Image control, can you show me some example codes as a reference?
Vincent Maverick Durano 5-Aug-16 6:46am    
That's very simple actually. You just need to grab the source from the selected Image and set it to your Image control like this:

Image img = (Image)GridView1.SelectedRow.FindControl("TheImageID");
if(img != null){
Image1.ImageUrl = img.ImageUrl;
}
abdujalilc 5-Aug-16 7:09am    
Thanks it worked. Is it possible now to retrieve and save the loaded image in Image control to DB? Please with some examples.

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