开发者

Insert and Select from Updated table simultaneously on the same Button click.(ASP.NET(C#))

I have an Insert command which insert values into myTable then immediately I want to to select that values from myTable which are inserted by the above insert.

What should I do to insert values and refresh then select command should execute on the single Button click.

  for (int l = 0; l < length; l++)
    {

       // Label1.Text += fid[l];
        FID[l] += Convert.ToInt32(FID1[l]);
       //Label1.Text += FID[l].ToString();

        SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
        SqlCommand comm = new SqlCommand("Insert into ContentManagement(CreatedBy,ContentTypeId,SubjectName,CreatedDate,FileId,IsArchieved,SessionId,UpdatedBy,LastUpdation,IsDeleted,IsFinalApproved) values(" + memid + ",'" + tpd + "','" + ListBox3.Text + "','" + DateTime.Now + "','" + FID[l] + "','" + z + "','" + session + "','" + memid + "','" + Da开发者_运维百科teTime.Now + "','" + z + "','" + z + "')", conn);

        try
        {
            conn.Open();
            comm.ExecuteNonQuery();
            //Label1.Text += "SUCCESS";
        }
        catch
        {
        conn.Close();
        }
        SqlConnection conn1 = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
        SqlCommand comm1 = new SqlCommand("Select ContentId from ContentManagement where CreatedBy=" + memid + " And ContentTypeId=" + tpd + " And SubjectName='" + ListBox3.Text + "'And FileId=" + FID[l] + "And SessionId=" + session, conn1);

       // try
        //{
            conn1.Open();
            SqlDataReader rdr1 = comm1.ExecuteReader();
            while (rdr1.Read())
            {
                cntid = Convert.ToInt32(rdr1["ContentId"]);
                Label1.Text += cntid.ToString();
            }
        //}
        //catch
        //{
            conn1.Close();
        //}
        }


You can do using Stored Procdedure. In that SP first insert data in table then write select statement.

For ex.

CREATE PROCEDURE [dbo].[sp1] 
 @Param1 VARCHAR(100)            
AS        

INSERT INTO table1 VALUES (@Param1)

SELECT * FROM table1


Wtite all the queries in the "Stored Procedures" and execute the Stored Procedures from button click


CREATE PROCEDURE [dbo].[sp1] 
 @Param1 VARCHAR(100)            
AS        

INSERT INTO table1 VALUES (@Param1)

SELECT top 1 * FROM table1 where memid orderby DESC
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜