Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
Certificate No   3435
   stud_id        5324
   Cmn_Minor_code      ROC
   Start Date       calendar1
   Issue Date       calendar2

   Button(Insert)

when i click Insert Button the above records are inserted in the database.

But i want to show the when i click the insert Button that records are to be shown in Gridview also.

for that how can i do using.

Insert Button code as follows
C#
try
{
    sql = "select stud_id from Tb_Past_Certificate_details where Stud_ID = '" + Label2.Text + "' and Cmn_Minor_code = '" + DdlCourseName.SelectedItem.Text + "' and PCD_Active <> 'D' ";
    drcoursea = drr.ExecRdr(sql);
    if (drcoursea.HasRows == true)
    {
        Label23.Text = "Already seleceted course is entered";
    }

    else
    {
        sql = "insert into Tb_Past_Certificate_details values('" + TxtCertNo.Text.ToString().Trim() + "','" + Label2.Text.ToString().Trim() + "','" + DdlCourseName.Text.ToString() + "','" + startdate.ToString() + "','" + issuedate.ToString() + "','Online','" + DateTime.Now.ToString() + "','F')";
        drr.InserData(sql);
        Label23.Text = "Records Inserted Successfully";
    }
}
catch (Exception ex)
{
    Label23.Text = ex.ToString();
    return;
}

i want to show when i click the insert button, the above records are to be shown in gridview simutaneously.
Posted
Updated 18-Sep-13 20:57pm
v4
Comments
Thomas ktg 19-Sep-13 3:05am    
Do you want to show only the last record inserted in the database or all the records inserted in the database in gridview?

SQL
BEGIN TRY
--Insert QUERY
INSERT INTO TABLE_NAME (...) VALUES (...)

--SELECT QUERY
-- You can alternatively use IF with SCOPE_IDENTITY as well. (instead to have TRY CATCH)
SELECT A, B, C FROM TABLE_NAME
WHERE CRITERIA = @CRITERIA

END TRY

BEGIN CATCH

RAISERROR('ROW COULD NOT BE INSERTED',10,1)

END CATCH




After this code. Just run it as a stored procedure and get result set. After finding the resultset with you, just bind your grid with it.
Grid will be updated after every insertion.
i.e.
C#
DataSet ds = DataAccess.Students.InsertStudent(objStudent);
if(ds != null && ds.Tables.Count >0)
{
Grid.DataSource = ds; //ds.Tables[0];
Grid.DataBind();
}
 
Share this answer
 
v2
you are binding data to grid view at page load

then you have to bind data again to grid view after you insert data to database then it will show data that you have inserted in database
 
Share this answer
 
C#
try
        {
            sql = "select stud_id from Tb_Past_Certificate_details where Stud_ID = '" + Label2.Text + "' and Cmn_Minor_code = '" + DdlCourseName.SelectedItem.Text + "' and PCD_Active <> 'D' ";
            drcoursea = drr.ExecRdr(sql);
            if (drcoursea.HasRows == true)
            {
                Label23.Text = "Already seleceted course is entered";
            }

            else
            {
                sql = "insert into Tb_Past_Certificate_details values('" + TxtCertNo.Text.ToString().Trim() + "','" + Label2.Text.ToString().Trim() + "','" + DdlCourseName.Text.ToString() + "','" + startdate.ToString() + "','" + issuedate.ToString() + "','Online','" + DateTime.Now.ToString() + "','F')";
                drr.InserData(sql);
//After inserting your record above you must write a select query to get the records to be bound in the grid.
                sql = "select * from Tb_Past_Certificate_details";
                DataTable dtable = drr.ExecRdr(sql); // Use the function to get the records.
                GridView1.DataSource = dtable; //Bind the datatable to the gridview datasource.
                GridView1.DataBind(); //Bind the gridview.

                Label23.Text = "Records Inserted Successfully";
            }
        }
        catch (Exception ex)
        {
            Label23.Text = ex.ToString();
            return;
        }
 
Share this answer
 
C#
try
    {
        sql = "select stud_id from Tb_Past_Certificate_details where Stud_ID = '" + Label2.Text + "' and Cmn_Minor_code = '" + DdlCourseName.SelectedItem.Text + "' and PCD_Active <> 'D' ";
        drcoursea = drr.ExecRdr(sql);
        if (drcoursea.HasRows == true)
        {
            Label23.Text = "Already seleceted course is entered";
        }

        else
        {
            sql = "insert into Tb_Past_Certificate_details values('" + TxtCertNo.Text.ToString().Trim() + "','" + Label2.Text.ToString().Trim() + "','" + DdlCourseName.Text.ToString() + "','" + startdate.ToString() + "','" + issuedate.ToString() + "','Online','" + DateTime.Now.ToString() + "','F')";
            drr.InserData(sql);
            Label23.Text = "Records Inserted Successfully";
//after that add this code
  sql = "select * from Tb_Past_Certificate_details";

                DataTable dtable = drr.ExecRdr(sql); // Use the function to get the records.
                GridView1.DataSource = dtable; //Bind the datatable to the gridview datasource.
                GridView1.DataBind(); //Bind the gridview.
        }
    }
    catch (Exception ex)
    {
        Label23.Text = ex.ToString();
        return;
    }
 
Share this answer
 
after pressing the insert button call the method which binds the data to the grid at the end
of your button click event before coming out from the function
 
Share this answer
 
after
Label23.Text = "Records Inserted Successfully";
in your else part code

bind your gridview again then it will show the inserted data
 
Share this answer
 
Comments
Dholakiya Ankit 19-Sep-13 2:56am    
read question carefully

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