Description: An
error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.new_page_aspx' does not contain a definition for 'DataList1_SelectedIndexChanged' and no extension method 'DataList1_SelectedIndexChanged' accepting a first argument of type 'ASP.new_page_aspx' could be found (are
you missing a using directive or an assembly reference?)
Source Error:
Line 3: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
Line 4:
Line 5: <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Line 6: RepeatDirections="Horizontal" Width="100%" BorderColor="#336699"
Line 7: BorderStyle="Solid" BorderWidth="2px"
Source File: c:\Users\jformento\Documents\Visual Studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx Line: 5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace TestFileUpload
{
public partial class New_Page : System.Web.UI.Page
{
I really appreciate your help! Now I get the following exception.
Server Error in '/' Application.
Value cannot be null.
Parameter name: String
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: String
Source Error:
Line 35: SqlCommand com = new SqlCommand(query, con);
Line 36:
Line 37: com.Parameters.Add("@ProductName", SqlDbType.Int).Value = Int32.Parse(id);
Line 38:
Line 39: con.Open();
Source File: C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx.cs Line: 37
Stack Trace:
[ArgumentNullException: Value cannot be null.
Parameter name: String]
System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9591070
System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119
System.Int32.Parse(String s) +23
TestFileUpload.New_Page.Page_Load(Object sender, EventArgs e) in C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx.cs:37
System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int UserId = int.Parse(context.Request.QueryString["UserId"]);
EFF_Database.Image image = new ImageManager(UserId).GetOneImage(int.Parse(context.Request.QueryString["id"]));
Code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
namespace TestFileUpload
{
public partial class ViewImage : System.Web.UI.Page
{
string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindFiles();
}
}
/// <summary>
/// Binds the files.
/// </summary>
private void BindFiles()
{
DataTable table = new DataTable();
// get the connection
using (SqlConnection conn = new SqlConnection(_connStr))
{
// write the sql statement to execute
string sql = "SELECT ProductID, ProductName, ProductImage FROM Products";
// instantiate the command object to fire
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
// get the adapter object and attach the command object to it
using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
{
// fire Fill method to fetch the data and fill into DataTable
ad.Fill(table);
}
// DataAdapter doesn't need open connection, it takes care of opening and closing the database connection
}
}
GridView1.DataSource = table;
GridView1.DataBind();
}
}
}
ASHX:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace TestFileUpload
{
/// <summary>
/// Summary description for ShowImage
/// </summary>
public class ShowImage : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["ProductID"] == null) return;
string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
string ProductID = context.Request.QueryString["ProductID"];
using (SqlConnection conn = new SqlConnection(connStr))
{
using (SqlCommand cmd = new SqlCommand("SELECT ProductImage FROM Products WHERE ProductID = @ProductId", conn))
{
cmd.Parameters.Add(new SqlParameter("@ProductID", ProductID));
conn.Open();
using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
reader.Read();
context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("ProductImage")]);
reader.Close();
}
}
}
}
public bool IsReusable
{
get
{
return true;
}
}
}
}
Now I receive this exception:
Server Error in '/' Application.
Instance failure.
Description: An
unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Line 45: {
Line 46: // fire Fill method to fetch the data and fill into DataTable
Line 47: ad.Fill(table); Line 48: }
Line 49: // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection
Source File: C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\ViewImage.aspx.cs Line: 47
jpf5064
0 Points
7 Posts
Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 06:54 PM|LINK
Hi,
I am getting the following exception:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'ASP.new_page_aspx' does not contain a definition for 'DataList1_SelectedIndexChanged' and no extension method 'DataList1_SelectedIndexChanged' accepting a first argument of type 'ASP.new_page_aspx' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 3: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> Line 4: Line 5: <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" Line 6: RepeatDirections="Horizontal" Width="100%" BorderColor="#336699" Line 7: BorderStyle="Solid" BorderWidth="2px"Source File: c:\Users\jformento\Documents\Visual Studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx Line: 5
Here is my Asp.net code:
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="New Page.aspx.cs" Inherits="TestFileUpload.New_Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"></asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="3"
RepeatDirections="Horizontal" Width="100%" BorderColor="#336699"
BorderStyle="Solid" BorderWidth="2px"
onselectedindexchanged="DataList1_SelectedIndexChanged">
<ItemTemplate>
<asp:label ID="Label1" runat="server" Text='<%# Eval ("ProductName") %>' Font-Bold="true" Font-Size="10pt" ForeColor="336699" Width="100%" />
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetImage.aspx?id=" + Eval("ProductID") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:DataList>
</asp:Content>
Here is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace TestFileUpload
{
public partial class New_Page : System.Web.UI.Page
{
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (!Page.IsPostBack)
{
string constr = "Server=SampleServer; Database=SampleDB; uid=test; pwd=test";
string query = "SELECT ProductID, ProductName FROM Products";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
DataList1.DataSource = table;
DataList1.DataBind();
}
}
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string constr = "Data Source=.\\SQLEXPRESS; Database=MedIns; uid=jformento; pwd=bluesky";
string query = "SELECT ProductImage FROM Products WHERE ProductID = @ProductID";
SqlConnection con = new SqlConnection(constr);
SqlCommand com = new SqlCommand(query, con);
com.Parameters.Add("@ProductName", SqlDbType.Int).Value = Int32.Parse(id);
con.Open();
SqlDataReader r = com.ExecuteReader();
if (r.Read())
{
byte[] imgData = (byte[])r["ProductImage"];
Response.BinaryWrite(imgData);
}
con.Close();
}
}
}
Any help would be great!
Thanks
C sql
UstesG
Contributor
2098 Points
449 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 07:05 PM|LINK
where is this event:
onselectedindexchanged="DataList1_SelectedIndexChanged" ?
But don't expect me to do your job!
jpf5064
0 Points
7 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 07:09 PM|LINK
No there currently. Where would you put it? This is the first application I have ever made and am new to this. Any help would be greatly appreciated.
Thanks
John
UstesG
Contributor
2098 Points
449 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 07:11 PM|LINK
add this to your code behind:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) { }But don't expect me to do your job!
jpf5064
0 Points
7 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 07:42 PM|LINK
I made the following changes, but still get the same exception:
protected void DataList1_SelectedIndexChanged (object source, EventArgs e)
{
if (!Page.IsPostBack)
{
string constr = "Server=SampleServer; Database=SampleDB; uid=test; pwd=test";
string query = "SELECT ProductID, ProductName FROM Products";
SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
DataList1.DataSource = table;
DataList1.DataBind();
}
}
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["id"];
string constr = "Data Source=.\\SQLEXPRESS; Database=MedIns; uid=jformento; pwd=bluesky";
string query = "SELECT ProductImage FROM Products WHERE ProductID = @ProductID";
SqlConnection con = new SqlConnection(constr);
SqlCommand com = new SqlCommand(query, con);
com.Parameters.Add("@ProductName", SqlDbType.Int).Value = Int32.Parse(id);
con.Open();
SqlDataReader r = com.ExecuteReader();
if (r.Read())
{
byte[] imgData = (byte[])r["ProductImage"];
Response.BinaryWrite(imgData);
}
con.Close();
}
UstesG
Contributor
2098 Points
449 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 08:49 PM|LINK
I copied your code and added the vent and I do not get the error.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" RepeatColumns="3" RepeatDirections="Horizontal" Width="100%" BorderColor="#336699" BorderStyle="Solid" BorderWidth="2px" onselectedindexchanged="DataList1_SelectedIndexChanged"> <ItemTemplate> <asp:label ID="Label1" runat="server" Text='<%# Eval ("ProductName") %>' Font-Bold="true" Font-Size="10pt" ForeColor="336699" Width="100%" /> <br /> <asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetImage.aspx?id=" + Eval("ProductID") %>' /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" /> </asp:DataList> </div> </form> </body> </html>code behind:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; public partial class Default2 : System.Web.UI.Page { protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e) { if (!Page.IsPostBack) { string constr = "Server=SampleServer; Database=SampleDB; uid=test; pwd=test"; string query = "SELECT ProductID, ProductName FROM Products"; SqlDataAdapter da = new SqlDataAdapter(query, constr); DataTable table = new DataTable(); da.Fill(table); DataList1.DataSource = table; DataList1.DataBind(); } } protected void Page_Load(object sender, EventArgs e) { string id = Request.QueryString["id"]; string constr = "Data Source=.\\SQLEXPRESS; Database=MedIns; uid=jformento; pwd=bluesky"; string query = "SELECT ProductImage FROM Products WHERE ProductID = @ProductID"; SqlConnection con = new SqlConnection(constr); SqlCommand com = new SqlCommand(query, con); com.Parameters.Add("@ProductName", SqlDbType.Int).Value = Int32.Parse(3); con.Open(); SqlDataReader r = com.ExecuteReader(); if (r.Read()) { byte[] imgData = (byte[])r["ProductImage"]; Response.BinaryWrite(imgData); } con.Close(); } protected void DataList1_SelectedIndexChanged(object sender, EventArgs e) { } }But don't expect me to do your job!
jpf5064
0 Points
7 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 09:00 PM|LINK
Server Error in '/' Application. Value cannot be null. Parameter name: String Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: String Source Error: Line 35: SqlCommand com = new SqlCommand(query, con); Line 36: Line 37: com.Parameters.Add("@ProductName", SqlDbType.Int).Value = Int32.Parse(id); Line 38: Line 39: con.Open(); Source File: C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx.cs Line: 37 Stack Trace: [ArgumentNullException: Value cannot be null. Parameter name: String] System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) +9591070 System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) +119 System.Int32.Parse(String s) +23 TestFileUpload.New_Page.Page_Load(Object sender, EventArgs e) in C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\New Page.aspx.cs:37 System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35 System.Web.UI.Control.OnLoad(EventArgs e) +91 System.Web.UI.Control.LoadRecursive() +74 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207devananddhag...
Member
222 Points
31 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 09:59 PM|LINK
You need httphandler for this,
Please refer below code and ref link.
HTML:
<img src='../Shared/ImageHandler.ashx?id=<%#Eval("ImageID") %>&UserId=<%=Session["LoggedInUserId"].ToString() %>' width="120" height="100" style="border:0;" />
httpHandler:
public class ImageHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
int UserId = int.Parse(context.Request.QueryString["UserId"]);
EFF_Database.Image image = new ImageManager(UserId).GetOneImage(int.Parse(context.Request.QueryString["id"]));
byte[] imgbytes = image.ImageFile;
string imgtype = null;
if (image.ImageType != null)
imgtype = image.ImageType.Name;
try
{
if (imgtype == "PNG")
{
using (MemoryStream ms = new MemoryStream(imgbytes))
{
using (Bitmap bm = new Bitmap(System.Drawing.Image.FromStream(ms)))
{
bm.Save(context.Response.OutputStream, ImageFormat.Png);
}
}
}
else if (imgtype == "JPG")
{
using (MemoryStream ms = new MemoryStream(imgbytes))
{
using (Bitmap bm = new Bitmap(System.Drawing.Image.FromStream(ms)))
{
bm.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
else
{
context.Response.ContentType = imgtype;
context.Response.BinaryWrite(imgbytes);
}
}
catch (Exception ex)
{
//handle the exeption
}
context.Response.End();
}
}
Ref:
http://www.codeproject.com/Articles/34084/Generic-Image-Handler-Using-IHttpHandler
Deva
Please remember to click "Mark as Answer" on the post that helps you
UstesG
Contributor
2098 Points
449 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 05, 2012 10:08 PM|LINK
check you sql parameter names and values.
But don't expect me to do your job!
jpf5064
0 Points
7 Posts
Re: Help with viewing binary image in sql DB with ASP.net
Nov 06, 2012 06:42 PM|LINK
I have made the following changes to my code:
ASP.net <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ViewImage.aspx.cs" Inherits="TestFileUpload.ViewImage" %> <asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <asp:GridView ID="GridView1" runat="server" EnableViewState="false" AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="Product ID" DataField="ProductId" /> <asp:BoundField HeaderText="Product Name" DataField="ProductName" /> <asp:TemplateField HeaderText="Image"> <ItemTemplate> <img src="ShowImage.ashx?autoId=<%# Eval("ProductID") %>" alt="<%#Eval("ProductName") %>" /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </asp:Content>Code behind: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; using System.IO; using System.Configuration; namespace TestFileUpload { public partial class ViewImage : System.Web.UI.Page { string _connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindFiles(); } } /// <summary> /// Binds the files. /// </summary> private void BindFiles() { DataTable table = new DataTable(); // get the connection using (SqlConnection conn = new SqlConnection(_connStr)) { // write the sql statement to execute string sql = "SELECT ProductID, ProductName, ProductImage FROM Products"; // instantiate the command object to fire using (SqlCommand cmd = new SqlCommand(sql, conn)) { // get the adapter object and attach the command object to it using (SqlDataAdapter ad = new SqlDataAdapter(cmd)) { // fire Fill method to fetch the data and fill into DataTable ad.Fill(table); } // DataAdapter doesn't need open connection, it takes care of opening and closing the database connection } } GridView1.DataSource = table; GridView1.DataBind(); } } }ASHX: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Configuration; using System.Data; using System.Data.SqlClient; namespace TestFileUpload { /// <summary> /// Summary description for ShowImage /// </summary> public class ShowImage : IHttpHandler { public void ProcessRequest(HttpContext context) { if (context.Request.QueryString["ProductID"] == null) return; string connStr = ConfigurationManager.ConnectionStrings["ConnStr"].ToString(); string ProductID = context.Request.QueryString["ProductID"]; using (SqlConnection conn = new SqlConnection(connStr)) { using (SqlCommand cmd = new SqlCommand("SELECT ProductImage FROM Products WHERE ProductID = @ProductId", conn)) { cmd.Parameters.Add(new SqlParameter("@ProductID", ProductID)); conn.Open(); using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { reader.Read(); context.Response.BinaryWrite((Byte[])reader[reader.GetOrdinal("ProductImage")]); reader.Close(); } } } } public bool IsReusable { get { return true; } } } }Now I receive this exception:
Server Error in '/' Application.
Instance failure.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Instance failure.
Source Error:
Line 45: { Line 46: // fire Fill method to fetch the data and fill into DataTable Line 47: ad.Fill(table); Line 48: } Line 49: // DataAdapter doesn't need open connection, it takes care of opening and closing the database connectionSource File: C:\Users\jformento\documents\visual studio 2010\Projects\TestFileUpload\TestFileUpload\ViewImage.aspx.cs Line: 47
Stack Trace: