Sunday, September 21, 2008

how to upload a image to gridview?

















It is a tiny application about “How to upload images?”. User shall provide username, browse file from local machine and once clicks on “add” button the details are saved (database or file system).
Please find the code above for your reference
In the above source code you can observe that the file(image) uploaded by client is saved using a class “UserDetailsData” which is created exclusively to save or retrieve user details which is user name and relative of the uploaded image file at server.
There 2 ways of uploading image file.
1) If you use data base , you create a table with column say “Image” whose data type should be BLOB (Binary Large Object).
While inserting a row into this table you can insert the byte stream of the image file uploaded at this column and while retrieving you should type cast the blob data to byte[] array and save into a file whose src should be used to a img tag to display.
2) The other way in Image column which can be Varchar , you can just save the uploaded image and save to application server local disk and whose file path can be saved in this Image column.
While retrieving you can read the path and assign it directly to img tag as src.

In the above example I have done it using (2) but since I am not Orcale data base server , so I used serialization concept to save user name and image file path into a data table and then writing the state of data table to a text file (Object persistence).






Regarding form view, what ever component we use to display , it might be grid view or form view it does the same thing.In the example i stated which i have done for grid view.For list view




<form id="form1" runat="server">
<div>
<asp:Label ID="lblmsg" runat="server"></asp:Label>
<table width="50%"><tr><td colspan="3">
<center>Upload Images</center>


<asp:FormView ID="frm_Dets" runat="server" Width="100%">
<HeaderTemplate><table width="100%"><tr><td style="width:20%">UserName</td><td>Snap</td></tr></table></HeaderTemplate>
<ItemTemplate>
<table width="100%">
<tr><td style="width:20%"><asp:Label ID="lblUserName" runat="server" Text='<%#Eval("User_name") %>'></asp:Label></td>
<td><img id="img_Snap" runat="server" src='<%#Eval("Image_URL") %>' title='<%#Eval("User_name") %>' style="width:100px" />
</td></tr>
</table>
</ItemTemplate>
</asp:FormView>


<asp:GridView ID="grd_Dets" runat="server" AutoGenerateColumns="false" Width="100%">
<Columns>
<asp:TemplateField>
<HeaderTemplate>User Name</HeaderTemplate>
<ItemTemplate><asp:Label ID="lblUserName" runat="server" Text='<%#Eval("User_name") %>'></asp:Label></ItemTemplate>
<ItemStyle Width="20%" HorizontalAlign="center" />

</asp:TemplateField>
<asp:TemplateField >
<HeaderTemplate>Snap</HeaderTemplate>
<ItemTemplate>
<img id="img_Snap" runat="server" src='<%#Eval("Image_URL") %>' title='<%#Eval("User_name") %>' style="width:100px" />
</ItemTemplate>
<ItemStyle Width="80%" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
</td></tr>

<tr><td style="width:30%"><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
<td style="width:60%"><asp:FileUpload ID="fl_upload" runat="server" /></td>
<td><asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" /></td>
</tr>
</table>

</div>
</form>



private void BindGrid()
{
try
{
UserDetailsData objUserDetailsData = new UserDetailsData();
DataTable dt = objUserDetailsData.GetUserDetails(UserDtsFilePath);
//Created a seperate class called userdetailsdata.cs to read information from File which saves
//serialized databale about the username and url of the fileuploaded by user
if (dt != null)
{
//Binding to gridview.
grd_Dets.DataSource = dt;
grd_Dets.DataBind();

//Binding to formview
frm_Dets.DataSource = dt;
frm_Dets.DataBind();
}

}
catch
{
throw;
}
}

8 comments:

Chandra Shekhar said...

how to bind data to formview

Programming and Database Concepts said...

Hi chandra sekhar,
please find my comments regarding how to upload images for fomr view.

Chandra Shekhar said...

how to differentiate between user and administrator roles in login page.
"i need some pages that are accessable to admin but not to users".

Chandra Shekhar said...

how to deploy website which is having database.

Programming and Database Concepts said...

Hi chandra sekhar,
Regarding how to diferentiate between a user and administrator it all depends upon the available data for a user in your application data base.

There should be a seperate table that stores user name and roles.
So that when the user loggins into application you should query this roles table and get his priviliges and store in a session object.

Later depending upon the business requirements you can control a module being accessed by a user based upon his priviliges.


Ex:-

There can be a generic function defined which returns boolean (true/false) when you pass user namd and role as parameters.

public bool IsUserPriviliged(string strUserName,string strRoleName)
{

/*code to check either in session variable or directly query the data base with the given parameters as where clause.

Suggessted o query database only once while logging in and store in session varaiable*/
return true if user has privs else false;
}

on either page load of that specifc page or on click of link you can use this function, either to redirect to that page or show insufficient priviliges page.

Cheers

Programming and Database Concepts said...

Hi ,
Regaring "how to deploy a website having databse?"

On working at real time basis there can be application installed on a machine (application server) and database installed on another machine (database server).

So thing is when you depoly your application you install your web site at application server, configure your application server to a databse server (databse configuration tools by specific parties), then mention that data base connection string (i hope you define that in configuration (web.config in .net)
file of your application) in your application configuration file.
Cheers,

Chandra Shekhar said...

this is pavan kumar i am having a doubt in asp.net

what is user defined control

explain me with an example program

Programming and Database Concepts said...

hi pavan,

sorry again for this delay response. Can you please post you queries or answers from now in my latest blogs to get quick response.


Regarding User Controls look at blog
http://programmerfindings.blogspot.com/2008/10/what-is-user-control.html