Welcome

Welcome Anonymous

Monday, September 20, 2010

How to edit GridView manually

Filling, Editing, Updating, Deleting from a GridView is quite easy now.
Just follow these steps.
1. Open Visual Studio create a web based project.
2. Go to design view of Default.aspx page and then drag&drop a GridView control from standard Visual Studio Toolbox.
3.Right click on GridView goto properties and then disable AutoGeneratedColumns property to false.
4. Navigate to your source page (default.aspx).
then we will found the tags look like

asp:gridview id="GridView1" runat="server" autogeneratecolumns="False">

5. Now, open tab in gridview and wirte the following code.


6. Save the File. Now goto your codebehind file(Default.aspx.cs)
7. Replace your Page_Load event with the following

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
fillgrid();

}
}
8. And write the Grid method

private void fillgrid()
{
con.Open();
da = new SqlDataAdapter("select * from login", con);
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
con.Close();
}

9. For GridView Editing Right click on GridView goto events and click in GridViewRowEditing event, it will generates a GridView1_RowEditing method in your codebehind file.
10. Now, in codebehind file write the following code

int i = Convert.ToInt32(e.NewEditIndex);

LinkButton lb1 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton1");
LinkButton lb2 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton2");
LinkButton lb3 = (LinkButton)GridView1.Rows[i].FindControl("LinkButton3");
lb2.Visible = lb3.Visible = true;
lb1.Visible = false;
Label l1 = (Label)GridView1.Rows[i].FindControl("Label1");
Label l2 = (Label)GridView1.Rows[i].FindControl("label2");
l1.Visible = l2.Visible = false;

TextBox t1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
TextBox t2 = (TextBox)GridView1.Rows[i].FindControl("Textbox2");
t1.Visible = t2.Visible = true;

11. For GridViewUpdating right click on gridview goto properties and then events double click on
GridViewUpdating event it'll generates a GridView1_RowUpdating event in your codebehind file.
12. Write the following code in that method

int i = e.RowIndex;
TextBox t1 = (TextBox)GridView1.Rows[i].FindControl("TextBox1");
TextBox t2 = (TextBox)GridView1.Rows[i].FindControl("Textbox2");
con.Open();
SqlCommand cmd = new SqlCommand("update login set name='" + t1.Text + "' where psw='" + t2.Text + "'",con );
t1.Visible = t2.Visible = false;

cmd.ExecuteNonQuery();
con.Close();


Label l1 = (Label)GridView1.Rows[i].FindControl("Label1");
Label l2 = (Label)GridView1.Rows[i].FindControl("label2");
l1.Visible = l2.Visible = true;

refreshgrid();

That's it !! It is that much easy to EDITING GRIDVIEW MANUALLY.
Lets' try to start your own logic to modify GridView

How to Display Image from Database

//Retrieving image from Database
SqlCommand c = new SqlCommand();
c.CommandType = CommandType.StoredProcedure;
c.Connection = con;
c.CommandText = "sp_readImage";

int id =Convert.ToInt16( obj.executescalar("select max(photoid) from Photos"));


c.Parameters.AddWithValue("@id",id );
byte[] imagedata = (byte[])c.ExecuteScalar();
MemoryStream memory = new MemoryStream(imagedata);

// To display the image in the page itself
img =System.Drawing.Image .FromStream(memory);
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg );

// To Disaply image in image control(image1)

BinaryWriter t = new BinaryWriter(File.Open(Server.MapPath("Images\\sample.jpeg"),FileMode.Create ));
t.Write(imagedata);
t.Close();
Image1.ImageUrl = Server.MapPath("Images\\sample.jpeg");

How to store image in a database in binary format

Create one empty web page and do the following things...

1.Drag & Drop FileUpload Control and one Button from standard Toolbox
2.Double click on button control it'll generate a button click event. In that event write the following code
3.business_logic is a class file and which is used for opening database conections, executing database queries,etc. You can may even directly declare directly the connections, commands in codebehind file(*.cs,*.vb)

business_logic obj = new business_logic();

if (FileUpload1.HasFile)
{

// Storing image in Database
BinaryReader reader = new BinaryReader(FileUpload1.PostedFile.InputStream);
byte[] image = reader.ReadBytes(FileUpload1.PostedFile.ContentLength);

string temp= obj.assignconn();
con = new SqlConnection(temp);
con.Open();
cmd.Connection = con;
cmd.CommandText = "sp_imageInsert";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@img", image);
cmd.ExecuteNonQuery();
Label2.Visible = true;
Label2.Text = "File Successfully Uploaded";

how to call code behind function from javascript in asp.net

var str = '<%=deleteFile()%>'

where deleteFile() the function name in
your code behind file(*.cs,*.vb)

How to catch browser close event

There are two events

1) window.onbeforeunload
2) body onbeforeunload="alert('Browser is going to be close');"