Showing posts with label display. Show all posts
Showing posts with label display. Show all posts

Monday, March 26, 2012

How to save image in sql server and display that image in datagrid?

Hay Friend's

Can u plese send me the way how to save image in sql server and display that images in datagrid or other control also like Image control or Image control Button?? Plese send the coding in C#.

Thank's

Amit

try this link...it display image in data grid...

http://www.odetocode.com/Articles/172.aspx

and for further reference you can refer

http://forums.asp.net/p/1122135/1754824.aspx#1754824

|||Hi amit, thanks for ur link but I m not fully Satisfy.. so kindly Send me more solutions..Thank'sAmit|||Hi Amit, Thanks for ur Link, but I m not fully Satisfy. So kindly send me more solutions ThanksAmit|||

Hi,

You can use stream object to insert the image to your database. Here's the sample for you to refer.

1.Store image in database:

Int64 intImageSize;string strImageType;Stream ImageStream;// Gets the Size of the ImageintImageSize = PersonImage.PostedFile.ContentLength;// Gets the Image TypestrImageType = PersonImage.PostedFile.ContentType;// Reads the ImageImageStream = PersonImage.PostedFile.InputStream;byte[] ImageContent =new byte[intImageSize + 1];int intStatus;intStatus = ImageStream.Read(ImageContent, 0, intImageSize);// Create Instance of Connection and Command ObjectSqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));SqlCommand myCommand =new SqlCommand("sp_person_isp", myConnection);// Mark the Command as a SPROCmyCommand.CommandType = CommandType.StoredProcedure;// Add Parameters to SPROCSqlParameter prmPersonImage =new SqlParameter("@.PersonImage", SqlDbType.Image);prmPersonImage.Value = ImageContent;myCommand.Parameters.Add(prmPersonImage);SqlParameter prmPersonImageType =new SqlParameter("@.PersonImageType", SqlDbType.VarChar, 255);prmPersonImageType.Value = strImageType;myCommand.Parameters.Add(prmPersonImageType);try {myConnection.Open();myCommand.ExecuteNonQuery();myConnection.Close();Response.Write("New person successfully added!");}catch (SqlException SQLexc) {Response.Write("Insert Failed. Error Details are: " + SQLexc.ToString());}
2. Retrieving Images from SqlServer and displaying in a DataGrid
<asp:TemplateColumn HeaderText="Image"> <ItemTemplate> <asp:Image Width="150" Height="125" ImageUrl='<%# FormatURL(DataBinder.Eval(Container.DataItem, "PersonID"))%>' Runat=server /> </ItemTemplate></asp:TemplateColumn>
 
 
//And the method FormatURL (A Server side Function) is as follows.public string FormatURL(object strArgument){return ("readrealimage.aspx?id=" + strArgument);}//And the method FormatURL (A Server side Function) is as follows.public string FormatURL(object strArgument){return ("readrealimage.aspx?id=" + strArgument);}
 
///Database partpublic void Page_Load(object sender, EventArgs e){string strImageID = Request.QueryString("id");// Create Instance of Connection and Command ObjectSqlConnection myConnection =new SqlConnection(ConfigurationSettings.AppSettings("ConnectionString"));SqlCommand myCommand =new SqlCommand("Select PersonImageType, PersonImage from das_person_real_images Where PersonID=" + strImageID, myConnection);// I have used the select statement. But it would be much much better, if you //could write a small stored procedure which contains the above sql statement.// Mark the Command as a SPROC (in case, if you wrote the stored procedure//myCommand.CommandType = CommandType.StoredProceduretry {myConnection.Open();SqlDataReader myDataReader;myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);while ((myDataReader.Read())) {Response.ContentType = myDataReader.Item("PersonImageType");Response.BinaryWrite(myDataReader.Item("PersonImage"));}myConnection.Close();}catch (SqlException SQLexc) {}}
Hope that helps. Thanks.
|||

Hi, this is an interesting post. I am curious about if there is something similar you can do to display the SQL image field in a DataGrid in a Windows Forms application too?

Thanks in advance.

Jim

How to save a dataset to a sql server table?

Scenario: I have created a dataset from an excel file to display it on a screen. Now I would like to save this same dataset in a SQL Server table.

Any ideas would be greatly appreciated.

Thanks

Data Set = One or More table(s).

Create a table in SQL Server that has same headers (same number of columns and same data types).

Iterate through your dataset table(s): MyDataSet.Tables[i]

Read each column from that table, and save its value in SQL Server.

Idea:

Why not to store the table in SQL Server then show it to user (e.g. by using DTS or SSIS).

Good luck.

|||

Is there any other way than iteracting row by row using the Tables collection of the DataSet?

Note: I am using .NET 1.1

thanks.

|||

hi there.,

i agree with the previous post ... it is better to save / import the date from excel to sql server then to show the data to user... here is how you can import data from spreadsheet to sql... using SqlBulkCopy[if you are using ADO.NET 2.0]

Import / Export Excel Spreadsheet Data into SQL Server Database Table Using SqlBulkCopy

hope it helps./.

|||

Unfortunately, I am using ADO.NET 1.1

Any similar way of doing it? Or other ideas?

Thanks

|||

refer to link below...

How to import data from Excel to SQL Server


|||

Is there any reference to How to import data from excel to sql server database in ADO.NET, instead of microsoft how to ADO and VB6?

Thanks in advance.

|||

Hi,

Actually the solution provided by CS4Ever has already answered the problem. But I don't know what does your "other way than iteracting row by row using the Tables collection of the DataSet" mean?

You have mentioned that you want to use ADO.Net, you have created a DataSet, and you want to save it in the back-end database.

The question has been quite clear, the DataSet is in-memory data container, if you want to save the schema and data in your dataset to your database, you have to build the same table schemas in your database first, and then, calling the update method of dataadapter to update the back-end (In other words, import the data in dataset to back-end). Note, you should keep the status of all rows in dataset as "modified" which indicates that don't call AcceptChanges() before calling Update method.

If you are insist on finding a better way, please show us more details, your needs, or something like "the way in my mind", and reply.

Thanks.

Friday, February 24, 2012

HOW to Retrieve an image from sql server and display it in ASP.net using "imagemap or image

Ok, the problem is that , i have a field called "Attach" in sql of type image, when selecting it , the field is getting data of type BYTE(). which am being unable to display them on an Image on the panel.

using the following vb.net code:

'Dim sel2 As String

'Dim myCom As SqlCommand

'Dim conn As New SqlConnection

'Dim drr As SqlDataReader

'Dim image As System.Drawing.Image

'sel2 = "select * from attach where att_desc = '" & DropDownList1.SelectedItem().Text & "' and doc_code = " & w_doc_code & " and subcode = " & w_doc_subcode & " and doc_num= " & w_doc_num & " "

'conn.ConnectionString = ("server=developer01;uid=sa;password=aims;database=DVPSOC;timeout=45")

'myCom = New SqlCommand(sel2, conn)

'conn.Open()

'drr = myCom.ExecuteReader()

'If drr.Read Then

' Me.ImageMap1.ImageUrl = drr.Item("attach")

'End If

'conn.Close()

Am getting an exeption on the following line Me.ImageMap1.ImageUrl = drr.Item("attach")

saying: Conversion from type 'Byte()' to type 'String' is not valid.

knowing that i tried converting using ToString but it's not getting any output then.

thanks for your help.

a example in C# as below:

try

{

con = new SqlConnection(constr);
cmd = new SqlCommand("select photopath,Photo from employees where employeeid=14", con);
con.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
if (!dr.IsDBNull(1))
{
byte[] photo = (byte[])dr[1];
MemoryStream ms = new MemoryStream(photo);
pictureBox1.Image = Image.FromStream(ms);
}

}
}
catch (Exception ex)
{
dr.Close();
cmd.Dispose();
con.Close();

MessageBox.Show(ex.Message);
}

hope can help u a little

|||

Sorry but that is not the answer to the question. picturebox cannot be used in ASP.Net.

HOW to Retrieve an image from sql server and display it in ASP.net using "imagemap or image

Ok, the problem is that , i have a field called "Attach" in sql of type image, when selecting it , the field is getting data of type BYTE(). which am being unable to display them on an Image on the panel.

using the following vb.net code:

'Dim sel2 As String

'Dim myCom As SqlCommand

'Dim conn As New SqlConnection

'Dim drr As SqlDataReader

'Dim image As System.Drawing.Image

'sel2 = "select * from attach where att_desc = '" & DropDownList1.SelectedItem().Text & "' and doc_code = " & w_doc_code & " and subcode = " & w_doc_subcode & " and doc_num= " & w_doc_num & " "

'conn.ConnectionString = ("server=developer01;uid=sa;password=aims;database=DVPSOC;timeout=45")

'myCom = New SqlCommand(sel2, conn)

'conn.Open()

'drr = myCom.ExecuteReader()

'If drr.Read Then

' Me.ImageMap1.ImageUrl = drr.Item("attach")

'End If

'conn.Close()

Am getting an exeption on the following line Me.ImageMap1.ImageUrl = drr.Item("attach")

saying: Conversion from type 'Byte()' to type 'String' is not valid.

knowing that i tried converting using ToString but it's not getting any output then.

thanks for your help.

a example in C# as below:

try

{

con = new SqlConnection(constr);
cmd = new SqlCommand("select photopath,Photo from employees where employeeid=14", con);
con.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
if (!dr.IsDBNull(1))
{
byte[] photo = (byte[])dr[1];
MemoryStream ms = new MemoryStream(photo);
pictureBox1.Image = Image.FromStream(ms);
}


}
}
catch (Exception ex)
{
dr.Close();
cmd.Dispose();
con.Close();

MessageBox.Show(ex.Message);
}

hope can help u a little

|||

Sorry but that is not the answer to the question. picturebox cannot be used in ASP.Net.

HOW to Retrieve an image from sql server and display it in ASP.net using "imagemap or i

Ok, the problem is that , i have a field called "Attach" in sql of type image, when selecting it , the field is getting data of type BYTE(). which am being unable to display them on an Image on the panel.

using the following vb.net code:

'Dim sel2 As String

'Dim myCom As SqlCommand

'Dim conn As New SqlConnection

'Dim drr As SqlDataReader

'Dim image As System.Drawing.Image

'sel2 = "select * from attach where att_desc = '" & DropDownList1.SelectedItem().Text & "' and doc_code = " & w_doc_code & " and subcode = " & w_doc_subcode & " and doc_num= " & w_doc_num & " "

'conn.ConnectionString = ("server=developer01;uid=sa;password=aims;database=DVPSOC;timeout=45")

'myCom = New SqlCommand(sel2, conn)

'conn.Open()

'drr = myCom.ExecuteReader()

'If drr.Read Then

' Me.ImageMap1.ImageUrl = drr.Item("attach")

'End If

'conn.Close()

Am getting an exeption on the following line Me.ImageMap1.ImageUrl = drr.Item("attach")

saying: Conversion from type 'Byte()' to type 'String' is not valid.

knowing that i tried converting using ToString but it's not getting any output then.

thanks for your help.

a example in C# as below:

try

{

con = new SqlConnection(constr);
cmd = new SqlCommand("select photopath,Photo from employees where employeeid=14", con);
con.Open();
dr = cmd.ExecuteReader();
while(dr.Read())
{
if (!dr.IsDBNull(1))
{
byte[] photo = (byte[])dr[1];
MemoryStream ms = new MemoryStream(photo);
pictureBox1.Image = Image.FromStream(ms);
}


}
}
catch (Exception ex)
{
dr.Close();
cmd.Dispose();
con.Close();

MessageBox.Show(ex.Message);
}

hope can help u a little

|||

Sorry but that is not the answer to the question. picturebox cannot be used in ASP.Net.