Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi... i request my friend..
i have 3 field
1st field is auto increment
2nd is text field
3rd if reference key
this is my field details so..
how can i deleting data ,inserting data, updating data using grid view
help me please,, because i can't do anything with out it
Posted
Updated 13-Mar-12 0:10am
v2
Comments
Herman<T>.Instance 13-Mar-12 5:40am    
and now?
RAHOOL SONI 13-Mar-12 6:02am    
how can i do insert update delete... give the SQL query
RAHOOL SONI 13-Mar-12 6:04am    
i want insert update del's SQL query
OriginalGriff 13-Mar-12 5:48am    
1) Don't say things like "do hurry up" - some people will take it as rude. Which it is, in English.
2) When posting a question, it really helps if you tell us what the problem is. Your title is the only clue we have - we can't see your screen, or access your HDD, or read your mind. So give us details about what you are trying to do, and what problem you are having.
Use the "Improve question" widget to edit your question and provide better information.

You need to read about SQL Server and DML. Pick up a book and read.
 
Share this answer
 
Comments
Manfred Rudolf Bihy 13-Mar-12 6:03am    
Enough said! 5+ :)
Abhinav S 13-Mar-12 6:12am    
Thank you.
Shahin Khorshidnia 13-Mar-12 6:48am    
+5
Abhinav S 13-Mar-12 7:25am    
Thanks.
Pramod Harithsa 13-Mar-12 6:53am    
+5
C#
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }

    }


private void BindData()
    {
        string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";
        string query = "SELECT 1st_column,2nd_column,3rd_column FROM Table_Name";
        SqlDataAdapter da = new SqlDataAdapter(query, constr);
        DataTable table = new DataTable();
        da.Fill(table);
        GridView1.DataSource = table;
        GridView1.DataBind();

    }

protected void lnkadd_Click(object sender, EventArgs e)
    {

        String connectionString = @"Data Source=SQLEXPRESS2008;Initial Catalog=database_name;Integrated Security=True";

        SqlConnection thisConnection = new SqlConnection(connectionString);
        try
        {
            thisConnection.Open();
            String thisQuery = "INSERT INTO Table_Name(2nd_column,3rd_column) VALUES ('" + txt2nd_column.Text + "','" + txt3rd_column.Text + "')";

            SqlCommand thisCommand = new SqlCommand(thisQuery, thisConnection);
            thisCommand.ExecuteNonQuery();
            lblmessage.Text = "Record Added Successfully !!!";
            thisConnection.Close();

        }
        catch (SqlException exp)
        {
            lblmessage.Text = exp.Message;
            Console.WriteLine(exp.Message);
   }

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindData();
    }

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        e.Cancel = true;
        GridView1.EditIndex = -1;
        BindData();
    }

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex];

        TextBox txt2nd_column = (TextBox)row.FindControl("txt2nd_column");
        TextBox txt3rd_column = (TextBox)row.FindControl("txt3rd_column");

        int a = Int32.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string b = txt2nd_column.Text;
        string c = txt3rd_column.Text;

        UpdateProduct(a, b,c);

    }

private void UpdateProduct(int a, string b,string c)
    {
        try
        {
            string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
            string query = "UPDATE Table_Name SET 2nd_column = @b, 3rd_column = @c WHERE 1st_column = @a";


            SqlConnection con = new SqlConnection(constr);
            SqlCommand com = new SqlCommand(query, con);

            com.Parameters.Add("@a", SqlDbType.Int).Value = 1st_column;
            com.Parameters.Add("@b", SqlDbType.NVarChar).Value = 2nd_column;
            com.Parameters.Add("@c", SqlDbType.NVarChar).Value = 3rd_column;


            con.Open();
            com.ExecuteNonQuery();
            con.Close();


            GridView1.EditIndex = -1;
            BindData();
        }
        catch (Exception ex)
        {
            throw ex;
    }

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
	try
	{
	string constr = @"Data Source=SQLEXPRESS2008;Initial Catalog=Database;Integrated Security=True";
	string query = "DELETE FROM Table_Name WHERE 1st_column=@a";

	SqlConnection con = new SqlConnection(constr);
	SqlCommand cmd = new SqlCommand();

	cmd.Parameters.Add("@a", SqlDbType.Int).Value = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[1].Text)";
	cmd.Connection = con;

	con.Open();
	cmd.ExecuteNonQuery();

	con.Close();
	BindData();
	}
	catch(Exception ex)
	{
	    throw ex;
	}
  }
 
Share this answer
 
v2
Comments
RAHOOL SONI 13-Mar-12 10:08am    
THANKs my dear friend for helping me...

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