在c#中使用asp.net在gridview中显示的插入值 [英] inserted value to be shown in gridview using asp.net in c#

查看:65
本文介绍了在c#中使用asp.net在gridview中显示的插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Certificate No   3435
   stud_id        5324
   Cmn_Minor_code      ROC
   Start Date       calendar1
   Issue Date       calendar2

   Button(Insert)


当我点击插入按钮时,
将上述记录插入到数据库中。



但我想显示当我点击插入按钮时,记录也将显示在Gridview中。



我该如何使用。



按如下方式插入按钮代码


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

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想要显示当我点击插入按钮时,上述记录将同时显示在gridview中。


i want to show when i click the insert button, the above records are to be shown in gridview simutaneously.

推荐答案

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









此代码后。只需将其作为存储过程运行并获取结果集。在找到结果集之后,只需用你的网格绑定它。

网格将在每次插入后更新。

ie





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.

DataSet ds = DataAccess.Students.InsertStudent(objStudent);
if(ds != null && ds.Tables.Count >0)
{
Grid.DataSource = ds; //ds.Tables[0];
Grid.DataBind();
}


您在页面加载时将数据绑定到网格视图



然后将数据插入数据库后,必须再次将数据绑定到网格视图,然后显示已插入数据库的数据
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


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;
        }


这篇关于在c#中使用asp.net在gridview中显示的插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆