Click here to Skip to main content
15,300,160 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Home : System.Web.UI.Page
{
  //  SqlConnection con;
    SqlCommand cmd = new SqlCommand();
    SqlDataAdapter da = new SqlDataAdapter();
    DataSet ds = new DataSet();
    SqlConnection con = new SqlConnection(@"Data Source=....");

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        int intCountryID = Convert.ToInt32(ddlCountry.SelectedValue.ToString());
        string strCountry = txtlang.Text.Trim();
        string strDesc = txtDescription.Text.Trim();
        int intUserID = 1;
        DateTime DTDate = DateTime.Now.Date;

        if (txtlang.Text != string.Empty && txtDescription.Text != string.Empty)
        {
            cmd.Connection = con;
            cmd.CommandText = "insert into tbl_language values(" + intCountryID + ",'" + strCountry + "','" + strDesc + "'," + intUserID + ",'" + DTDate + "'," + intUserID + ",'" + DTDate + "',1)";
            cmd.ExecuteNonQuery();
            BindData();
        }
        else
        {
            ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Some fields are missing...')</script>");
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindData();


    }
    public void GridView_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();
    }
    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        BindData();
        
        TaskGridView.EditIndex = -1;
        BindData();
    }

    public void BindData()
    {
        con.Open();
        cmd = new SqlCommand("select language_id,language_name,language_desc from tbl_language where ref_country_id = " + Convert.ToInt32(ddlCountry.SelectedValue.ToString()), con);
        da = new SqlDataAdapter(cmd);
        da.Fill(ds);

        GridView1.DataSource = ds;
        GridView1.DataBind();
        GridView1.Visible = true;
        
        con.Close();
    }
}

This is my code ..
What I need is there is two tables tbl_country,
tbl_language
VB
(language_id
ref_country_id
language_name
language_desc
created_by
created_date
updated_by
updated_date
isactive )

..

if i click edit in grid it should be updated.
if i click delete it should be change it's field isactive to false state in tbl_language table.

*** All the codings i need in " .cs not in aspx "

Pls help me..
Thanks in advance..














Still i'm getting error..


protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//BindData();
 
int index = GridView1.EditIndex;
GridViewRow row = GridView1.Rows[index];
TextBox t1 = row.FindControl("TextBox2") as TextBox;
TextBox t2 = row.FindControl("TextBox3") as TextBox;
string t3 = GridView1.DataKeys[e.RowIndex].Value.ToString();
 

string cmdText = "Update tbl_language set language_name= '" + t1.Text + "' , language_desc= '" + t2.Text + "' where language_id='" + t3 + "'";
SqlCommand command = new SqlCommand(cmdText, con);


command.ExecuteNonQuery();
con.Close();
 


GridView1.EditIndex = -1; 
BindData();
}
 

public void BindData()
{
con.Open();
cmd = new SqlCommand("select language_id,language_name,language_desc from tbl_language where ref_country_id = " + Convert.ToInt32(ddlCountry.SelectedValue.ToString()), con);
da = new SqlDataAdapter(cmd);
da.Fill(ds);
 
GridView1.DataSource = ds;
GridView1.DataBind();
GridView1.Visible = true;

con.Close();
}
 

 

 

I'm getting error in rowupdating method...
line : string t3 = GridView1.DataKeys[e.RowIndex].Value.ToString();
Posted
Updated 19-Mar-14 22:47pm
v3
Comments
[no name] 20-Mar-14 1:54am
   
you have done everything in .cs side. So which of the code is done in aspx. and what you need tell it clearly
SViki 20-Mar-14 2:01am
   
the grid should be created in aspx..and mention the methods like edit,delete,cancel in aspx..
the coding for this edit ,cancel, delete i need in .cs
the above codings are i refered some web pages..

protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
BindData();

TaskGridView.EditIndex = -1;
BindData();
}

give an sample coding for this part.. and for row editing or deleting

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