C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Gridview upload


Posted Date: 18 Aug 2008    Resource Type: Code Snippets    Category: Databinding

Posted By: manivannan       Member Level: Gold
Rating:     Points: 10



This code will be useful to upload an image and change it.
use it.


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
//connection declared in public so we can use in any where;
SqlConnection con;
SqlCommand command;
SqlDataReader dr;
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{

//connection opened

con = new SqlConnection("Server=server;Initial Catalog=mani;uid=sa;pwd=sa;");
con.Open();
//passing the command
command = new SqlCommand("select * from login", con);
//excute reader executes one row(first row in the table)
dr = command.ExecuteReader();
while (dr.Read())
{
//condition checking;
if (Login1.UserName == dr["loginname"].ToString() && Login1.Password == dr["loginpassword"].ToString())
{

Response.Redirect("upload.aspx");//redirecting
}
else
{

Lbltry.Text = "INVALID ID TRY AGAIN";

}
}

con.Close();
dr.Close();



}
}
///////////////////////////////////////////////////////
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;


public partial class upload : System.Web.UI.Page
{
//declaring the connection objects
SqlConnection con;
SqlCommand cmd;
string caption;
string path;
decimal price;
string ab;


protected void Page_Load(object sender, EventArgs e)
{
lblsubmit.Visible = false;
Txtimageid.Enabled = false;

//auto generation numbers
call();

}
public void call()
{
con = new SqlConnection("Server=server;initial catalog=mani;uid=sa;pwd=sa");
cmd = new SqlCommand("select max(imageid)+1 imageid from upload", con);
con.Open();
ab = cmd.ExecuteScalar().ToString();
Txtimageid.Text = ab;
con.Close();
}


//protected void Btnback_Click1(object sender, EventArgs e)
//{
// Response.Redirect("Default.aspx");
//}
protected void Btnaddimage_Click(object sender, EventArgs e)
{
//giving physical connection to the server. it will convert into virtual address


string abc = Server.MapPath("\\WebSite1\\imagedown\\");//virtual folder name and my folder name
string file = "\\imagedown\\" + FileUpload1.FileName;//control name
caption = Txtcaption.Text;
price = Convert.ToDecimal((Txtprice.Text));//converting string to decimal.
path = FileUpload1.FileName;
if (FileUpload1.PostedFile != null)
{
// try
{
FileUpload1.PostedFile.SaveAs(abc + FileUpload1.FileName);
}
// catch (Exception) { }
}

con = new SqlConnection("Server=server;initial catalog=mani;uid=sa;pwd=sa");

cmd = new SqlCommand("insert into upload values(" +ab+",'" + caption + "','" + file + "'," + price + ")", con);
con.Open();
cmd.ExecuteNonQuery();
lblsubmit.Visible = true;
lblsubmit.Text = "Successfully Submited";
GridView1.DataBind();
clear();
call();
con.Close();



}
protected void Btnviewimage_Click(object sender, EventArgs e)
{
//getting the values using session

Session["urladdress"] = FileUpload1.PostedFile.FileName;
//code to create a window
Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.open('view.aspx', 'popup', 'width=400,height=400, menubar=no, scrollbars=no, toolbar=no, location=no, resizable=no, top=50, left=50');</script>");
}


public string ImageUrl(string imagepath)
{

if (imagepath != null && imagepath.Length > 0)

return ("~/" + imagepath);

else
return null;

}
public void clear()
{
call();
Txtcaption.Text = "";
Txtprice.Text = "";
}

protected void Btnorder_Click(object sender, EventArgs e)
{
decimal total = 0;
int i;
string str;
string[] order = new string[GridView1.Rows.Count];
Session["gridcount"] = GridView1.Rows.Count;
for ( i = 0; i < GridView1.Rows.Count; i++)
{
GridViewRow row = GridView1.Rows[i];
bool ischecked = ((CheckBox)row.FindControl("check")).Checked;
if (ischecked)
{

str = GridView1.Rows[i].Cells[2].Text;
total += Convert.ToDecimal(str);
Session["caption"+i] = GridView1.Rows[i].Cells[1].Text;
Session["price"+i] = GridView1.Rows[i].Cells[2].Text;
Session["total"] = total.ToString();
}

}

Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.open('Total.aspx', 'popup', 'width=400,height=400, menubar=no, scrollbars=no, toolbar=no, location=no, resizable=no, top=50, left=50');</script>");
}


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


int i;
i = int.Parse(e.CommandArgument.ToString());
if(e.CommandName.ToString() == "editimage")
Response.Redirect("iimageview.aspx?imageid=" + GridView1.Rows[i].Cells[0].Text.ToString());
}
}

/////////////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.SqlClient;
public partial class iimageview : System.Web.UI.Page
{
SqlConnection con;
SqlCommand cmd;

string get;

protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection("Server=server;initial catalog=mani;uid=sa;pwd=sa");
con.Open();
get=Request.QueryString.Get("imageid");
}
protected void Button1_Click(object sender, EventArgs e)
{
string abc = Server.MapPath("\\WebSite1\\imagedown\\");//virtual folder name and my folder name
string file = "\\imagedown\\" + FileUpload1.FileName;//control name
if (FileUpload1.PostedFile != null)
{
// try
{
FileUpload1.PostedFile.SaveAs(abc + FileUpload1.FileName);
}
// catch (Exception) { }
}

con = new SqlConnection("Server=server;initial catalog=mani;uid=sa;pwd=sa");
cmd = new SqlCommand("update upload set imagepath='"+file+"' where imageid="+get, con);
con.Open();
cmd.ExecuteNonQuery();
Response.Redirect("upload.aspx");

}
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("upload.aspx");
}
protected void Btnview_Click(object sender, EventArgs e)
{
Session["url"] = FileUpload1.PostedFile.FileName;
Response.Write("<script language=\"javascript\" type=\"text/javascript\">window.open('view.aspx', 'popup', 'width=400,height=400, menubar=no, scrollbars=no, toolbar=no, location=no, resizable=no, top=50, left=50');</script>");
}
}
/////////////////////////////////////
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class view : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Response.Write((string)Session["urladdress"]);;
Image1.ImageUrl = (string)Session["urladdress"];
Image1.ImageUrl = (string)Session["url"];
//Response.Write(Image1.ImageUrl);
}
}
/////////////////////////////////////////////////
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Total.aspx.cs" Inherits="Total" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
</head>

<body>
<form id="form1" runat="server">
<%
//string tot = Request.Form.Get("txtfrmamt");
string tot = Session["total"].ToString();
%>
<div>
<table style="width: 401px; height: 29px">
<tr>
<td align="center" colspan="3">
Selected Products</td>
</tr>
<tr>
<td align="center" colspan="3">
</td>
</tr>
<tr>
<td style="width: 103px">
</td>
<td style="width: 153px">
<strong>Caption</strong></td>
<td>
<strong>Price</strong></td>
</tr>
<tr>
<td style="width: 103px">
</td>
<td style="width: 153px">
</td>
<td>
</td>
</tr>
<%for (int i = 0; i <= (Int16)Convert.ToInt16(Session["gridcount"].ToString()); i++)
{
if ((string)Session["caption"+i] != null)
{ %>
<tr>
<td style="width: 103px">
</td>
<td style="width: 153px">
<%=(string)Session["caption"+i]%>
</td>
<td align="left">
<%=(string)Session["price" + i]%>
</td>
</tr>
<%}
}%>
<tr>
<td style="width: 103px">
</td>
<td style="width: 153px" align="right">Total Amount = </td>
<td><%=tot%></td>

</tr>
</table>
</div>
</form>
</body>
</html>




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Image upload  .  Gridview  .  Data binding  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Enable and Disable a Control by Binding to another Control
Previous Resource: Binding Combobox with Subdirectory Name and Path of Specific Directory
Return to Discussion Resource Index
Post New Resource
Category: Databinding


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

UK Conference calling Company

Contact Us    Privacy Policy    Terms Of Use