Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Text;
using System.Drawing;


public partial class Approver : System.Web.UI.Page
{
    SqlConnection cn = new SqlConnection("server='fre-ggn-fstest\\sharepoint'; database = 'DMS1'; integrated security = 'true';");
    SqlDataAdapter sda;
    DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        cn.Open();
        FillData();
    }

    public void FillData()
    {
        string str = "select * from tdocument";
        sda = new SqlDataAdapter(str, cn);
        ds = new DataSet();
                
        sda.Fill(ds);
        cn.Close();
        GVDetails.DataSource = ds;
        GVDetails.DataBind();


        Button apr = new Button();
        Button rej = new Button();

        apr.Text = "APPROVE";
        rej.Text = "REJECT";
        
        
        GVDetails.Controls.Add(apr);
        GVDetails.Controls.Add(rej);
  // FROM HERE I WANT TO ADD THE CODE FOR THE 2 CREATED BUTTONS...... ON CLICKING THE "approve" THE STATUS FIELD IN DATABASE MUST BE CHANGED IN AS "approved"..... AND ON CLICKING THE ..... "reject" BUTTON THE STATUS IN THE DATABASE MUST BE ENTEERED AS "rejected"........      

    }
    
}
Posted
Updated 15-Feb-12 0:50am
v2
Comments
Karthik Harve 15-Feb-12 6:50am    
[Edit] pre tags added.
Ed Nutting 15-Feb-12 6:53am    
Tried adding an event handler to the buttons' click events? e.g. apr.Click += new EventHandler

As you mentioned above you have added two buttons. Like:

C#
apr.Text = "APPROVE";
rej.Text = "REJECT";


//Add the command name here to work with this buttons.
//You can also add a command argument if required
apr.CommandName="Approve";
rej.CommandName="Reject";

GVDetails.Controls.Add(apr);
GVDetails.Controls.Add(rej);


Now go to the grid view RowCommand event and filter your commands. Like:

C#
protected void gvMyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Reject")
    {
        //Your code goes here
    }
    else if (e.CommandName == "Approve")
    {
       //Your code goes here
    }
}



Try this.. Accept the answer if you like..
All the best.:)
 
Share this answer
 
v3
Comments
[no name] 15-Feb-12 7:16am    
Solution.. But still not got a single star.. Strange. :(
shweta9999 15-Feb-12 22:18pm    
u r a star buddy...
[no name] 15-Feb-12 22:33pm    
Thanx dear... :)
shweta9999 15-Feb-12 22:56pm    
thankyou so much Amit... i really appriciate your job....


Amit please aslo tell me how can i run the SQL commands inside the if condition.... one small example would be appriciated..

and please tell me how can i use or add a command argument to my code ...


thanks
shweta9999 15-Feb-12 23:10pm    
hi amit, i tryed your code but....these two errors are occuring.....

i hope you will prove the solution..... thanks

Error 17 Cannot implicitly convert type 'string' to 'bool' C:\shweta_development\r&d\DMS1_pro\Approver.aspx.cs 54 13 C:\...\DMS1_pro\

Error 18 Property or indexer 'System.Web.UI.WebControls.CommandEventArgs.CommandName' cannot be assigned to -- it is read only C:\shweta_development\r&d\DMS1_pro\Approver.aspx.cs 54 13 C:\...\DMS1_pro\
Hi,

you can do this as well,

if you have dropdownlist then

C#
ddlDropDown2.OnSelectedIndexChanged += new EventHandler(this.ddlDropDown2_SelectedIndexChanged);


Here you can see dropdown selectIndexChaged event is created and it's handler is also created. similarly you can do it for button.

thanks
-amit.
 
Share this answer
 
I am giving all codes.. Check it now yaar..
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Data Source=IBM\\IBMDB;Initial Catalog=Testing;Persist Security Info=True;User ID=admin;Password=admin@2011");
    protected void Page_Load(object sender, EventArgs e)
    {
        fillGrid();
    }

    public void fillGrid() {
        con.Open();
        SqlCommand com = new SqlCommand("Select * from tbLogin", con);
        SqlDataAdapter sda = new SqlDataAdapter(com);
        DataSet ds = new DataSet();
        sda.Fill(ds);
        com.Dispose();
        sda.Dispose();
        con.Close();
        //Adding two columns in datatable.
        ds.Tables[0].Columns.Add("Approv");
        ds.Tables[0].Columns.Add("Reject");

        GridView1.DataSource = ds;
        GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //Adding the buttons
        Button b1 = new Button();
        Button b2 = new Button();
        b1.Text = "Approved";
        b2.Text = "Reject";

        //assignnig command name
        b1.CommandName = "Approved";
        b2.CommandName = "Reject";

        if (e.Row.RowType == DataControlRowType.DataRow) {
            e.Row.Cells[3].Controls.Add(b1);
            e.Row.Cells[4].Controls.Add(b2);
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Approved") {
            Response.Write("Aproved");
        }
        else if (e.CommandName == "Reject") {
            Response.Write("Reject");
        }
    }
}


Reply me if you got the exact answer.. And ya you can modify this according to your requirement..

Amit Kumar
 
Share this answer
 
hey amit... i have implemented this code with my project...

please tell me ... inside the if condition mentioned below... how can i run the SQL command to change the status from "PENDING" to "APPROVED"

just give me one simple example....

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Approved") {
Response.Write("Aproved"); // i m talking about here on
}
else if (e.CommandName == "Reject") {
Response.Write"Reject");
}
}


thankyou...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900