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



My Profile

Gifts

Active Members
TodayLast 7 Days more...







file uplaod and edit in datagrid


Posted Date: 11 May 2008    Resource Type: Code Snippets    Category: C# Syntax

Posted By: karthikeyan-The Great       Member Level: Gold
Rating:     Points: 10



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;
using sss;

public partial class doucupload : System.Web.UI.Page
{
string getconnection = ConfigurationManager.AppSettings["get"];
Class1 ob = new Class1();
SqlConnection con;
SqlDataReader dr;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds=new DataSet ();
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack!=true)
{
filldata();
}

}

protected void Button1_Click(object sender, EventArgs e)
{
if (hid.Value == "")
{
string url = Server.MapPath("..");
Random RandomClass = new Random();
int RandomNumber = RandomClass.Next(10, 99);
string filename = RandomNumber.ToString() + upfile.FileName;
upfile.PostedFile.SaveAs(url + "/upload/" + filename);

con = new SqlConnection(getconnection);
con.Open();
SqlParameter[] sp = new SqlParameter[3];

sp[0] = new SqlParameter("@title", SqlDbType.NVarChar, 50);
sp[0].Value = TextBox1.Text;

sp[1] = new SqlParameter("@filename", SqlDbType.NVarChar, 50);
sp[1].Value = filename;


sp[2] = new SqlParameter("@status", SqlDbType.Char, 10);
sp[2].Value = DropDownList1.SelectedValue;

cmd = new SqlCommand("fileup", con);
cmd.CommandType = CommandType.StoredProcedure;

foreach (SqlParameter sqlparams in sp)
{
cmd.Parameters.Add(sqlparams);
}
cmd.ExecuteNonQuery();
con.Close();
}
else
{
con = new SqlConnection(getconnection);
if (upfile.FileName == "")
{

cmd = new SqlCommand("update fileupload set title='" + TextBox1.Text + "',status='"+DropDownList1 .SelectedValue +"' where fileid='" + hid.Value + "'", con);
}
else
{
string url = Server.MapPath("..");
Random RandomClass = new Random();
int RandomNumber = RandomClass.Next(10, 99);
string filename = RandomNumber.ToString() + upfile.FileName;
upfile.PostedFile.SaveAs(url + "/upload/" + filename);
cmd = new SqlCommand("update fileupload set title='" + TextBox1.Text + "',filename='" + filename + "',status='" + DropDownList1.SelectedValue + "' where fileid='" + hid.Value + "'", con);
}
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

hid.Value = "";
filldata();
}
public void filldata()
{
con = new SqlConnection(getconnection);
da = new SqlDataAdapter ("select * from fileupload", con);
da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "ed")
{
con = new SqlConnection(getconnection);
cmd = new SqlCommand("select * from fileupload where fileid='"+e.CommandArgument +"'", con);
hid.Value = e.CommandArgument.ToString ();
con.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
TextBox1.Text = dr["title"].ToString();
DropDownList1.SelectedValue = dr["status"].ToString();

}
con.Close();
}
if (e.CommandName == "del")
{
con = new SqlConnection(getconnection);
cmd = new SqlCommand("delete from fileupload where fileid='" + e.CommandArgument + "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
filldata();
}
}




Responses

Author: Neeraj Saluja    12 May 2008Member Level: Gold   Points : 2
Providing some explanation and highlighting the key syntaxes will help the readers. Even if you can add code comments using "//", that would also add value to it.


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

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: How to determine a session expired or not
Previous Resource: Create an entry in the Eventlog
Return to Discussion Resource Index
Post New Resource
Category: C# Syntax


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design   Scripts

UK Conference calling Company

Contact Us    Privacy Policy    Terms Of Use