locked
How do I resolve the Insert issue RRS feed

  • Question

  • User-218090889 posted

    Hi Friends,

    I appreciate all of you who has been responding to my questions, you are making me great, thanks.

    My question now is:

    I has a button that is created dynamically in a GridView container, below is the click event for the button, and what I want it to do is, when the button is clicked, it will make a random select of Id from Table_A and return the result as variables then use the variables to make Insert into Table_B

    this is the code:

                    PlaceHolder ph = (PlaceHolder)e.Row.FindControl("phGame");

                    Button bt1 = new Button();
                    bt1.Text = "PalsCall";
                    bt1.EnableViewState = true;
                    ph.Controls.Clear();
                    ph.Controls.Add(bt1);

                    bt1.Click += delegate
                    {
                        
                        string ID1 = null;
                        string ID2 = null;
                        string ID3 = null;
                        string connetionString = null;
                        SqlConnection con  ;
                        SqlCommand cmd ;
                        string sql = null;
                        SqlDataReader rdr ;

                        connetionString = "Data Source=MyServerName;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False";
                        sql = "SELECT (Id) FROM Table_A WHERE (ABS(CAST((BINARY_CHECKSUM(Id, NEWID())) as int))% 10) < 4 ";

                         con = new SqlConnection(connetionString);
                        try
                         {
                         con.Open();
                         cmd = new SqlCommand(sql, con);
                         rdr = cmd.ExecuteReader();
                          while (rdr.Read())
                         {
                             ID1 = Convert.ToString(rdr.GetValue(0));
                             ID2 = Convert.ToString(rdr.GetValue(1));
                             ID3 = Convert.ToString(rdr.GetValue(2));
                        
                        }
                          rdr.Close();
                          cmd.Dispose();
                    
                        }
                          finally
                        {
                          con.Close();
                        }

                        string GameCalls = "INSERT INTO [Table_B] (PlayerId,ScID) VALUES('" + PlayerId + "','" + ID1 + "'),('" + PlayerId + "','" + ID2 + "'),('" + PlayerId + "','" + ID3 + "')";
                        dbClass.ConnectDataBaseToInsert(GameCalls);

                        

                        //ph.Controls.Clear();

                        Response.Redirect("~/GamePage.aspx?Id=" + Session["UserId"].ToString());        
                    };
                }

    When I ran the code it show no error, and it seems the button is not fired. What could be the problem.

    Note: ph is for PlaceHolder for the button in the GridView, and PlayerId is Input variable from a Label in the GridView

    Saturday, October 1, 2016 3:46 AM

All replies

  • User-2057865890 posted

    Hi Enzyme,

    When I ran the code it show no error, and it seems the button is not fired. What could be the problem.

    First, remove tr{} finally{}, set a function breakpoint in source code by clicking in the left margin of a source code file.

    Second, run this code in the debugger, check whether the breakpoint is hit.

    https://msdn.microsoft.com/en-us/library/5557y8b4.aspx 

    Best Regards,

    Chris

    Sunday, October 2, 2016 3:34 PM