Click here to Skip to main content
15,885,745 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a grid view. and in this i have a check box (in each row).and a text box also.
now i want to insert data of text box and some column(for example 'product type') into my database on click of a button that is outside grid view....
how can i do this also tell me stored procedure way to do this....

give some suggetion friends...
Posted
Comments
GDdixit 22-Dec-12 9:16am    
hello bro , problem is not that which link you refer me...i am asking about check box as i told detailed in my question.....and your link give no idea bout this...?

The stored procedure for this could be:
SQL
CREATE PROCEDURE [ProcedureName] 
	-- Add the parameters for the stored procedure here
	@Parameter1 INT
AS
BEGIN
    SET NOCOUNT ON;
	
    BEGIN TRANSACTION
    	
	-- if row alredy exists in a table update it else insert new row
    IF EXISTS ( SELECT  *
                FROM    [TableName]
                WHERE   <conditions> )
        BEGIN
            UPDATE  [TableName]
            SET     [FieldName] = [Value]
            WHERE   <conditions>
        END
    ELSE 
        BEGIN
	    INSERT INTO <tablename> VALUES <............>
        END
        
        
	 IF @@ROWCOUNT = 0 
        BEGIN
            ROLLBACK TRANSACTION
        END
    ELSE 
        BEGIN
            COMMIT TRANSACTION
        END
END


And in Click Event of a button do this:
C#
protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection conn = <your connectionstring="">;
            SqlCommand sqlCommand = new SqlCommand();
            sqlCommand.Connection = conn;

            String sqlQuery = "";

            try
            {
               conn.Open();

               for (int i = 0; i < yourdgv.RowCount - 1; i++)
               {
                 sqlCommand.CommandText = "<yourprocedurename>";
                 sqlCommand.CommandType = CommandType.StoredProcedure;
                 sqlCommand .Parameters.AddWithValue("@parameter1", <value>);

                 sqlCommand.ExecuteNonQuery();

                 //Data insertion completed.
               }
            }
            catch (Exception)
            {
                // your exception..
            }
            finally
            {
                conn.Close();
            }
        }


...
Plz. Let me know if this helps.
 
Share this answer
 
v3
use following code...
C#
protected void btnSaveData_Click(object sender, EventArgs e)
{
    foreach (GridViewRow row in grvYourGrid.Rows)
    {
        CheckBox chk = (CheckBox)row.FindControl("YourCheckBoxID");
        if (chk.Checked)
        {
            string textInTextBox =((TextBox)row.FindControl("YourTextBoxID")).Text;
            //same like for other controls
            //perform your database operation here
        }
    }
}
 
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