//What I would like to do here is instead of having to write multiple connection and parameter properties for all the connection function //I want to create another class just for the connection and on this just call that with parameters only. This should help in code reuse. //or may be better way of working with connection string to connect with database. public static List <AllPlayer> GetAllPlayer(int PlayerID) { List <AllPlayer> AllPlayer = new List <AllPlayer>(); string cs = ConfigurationManager.ConnectionStrings["DBConnectionString"].ConnectionString; using (SqlConnection con = new SqlConnection(cs)) { SqlCommand cmd = new SqlCommand("spGetAllPlayerforTeam", con); cmd.CommandType = CommandType.StoredProcedure; SqlParameter parameter = new SqlParameter("@TeamID", PlayerID); cmd.Parameters.Add(parameter); con.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { AllPlayer Players = new AllPlayer(); //Players.PlayerID = Convert.ToInt32(rdr["ID"]); Players.FullName = rdr["FLName"].ToString(); Players.Age = Convert.ToInt32(rdr["Age"]); Players.Position = rdr["Position"].ToString(); Players.Country = rdr["Country"].ToString(); Players.TeamName = rdr["TeamName"].ToString(); AllPlayer.Add(Players); } } return(AllPlayer); }
/*I know I have done something wrong so wrong here. The above method calls for the row edit and row select but the below code does * the updating. I have no clue which one is working but if I remove one it will error out. Breakpoints too lazy for that, the problem that I have is * I am trying to achieve row edit through drag and drop approach and update and insert through code behind file. I have no clue how to work around to * have the consistency but it works. I am happy. :) */ protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { //RowIndex, I will never forget this word never. Took me like weeks to find out the rowindex. To get the row index so that I can get the textbox //from textbox to get the text and set the value to the get and set properties. No joke took me ages. GridViewRow row = GridView1.Rows[e.RowIndex]; int playerid = Convert.ToInt32(Session["TeamID"]); TextBox fullname = (row.FindControl("txtfullName") as TextBox); TextBox age = (row.FindControl("txtAge") as TextBox); TextBox position = (row.FindControl("txtPosition") as TextBox); TextBox country = (row.FindControl("txtCountry") as TextBox); AllPlayer update = new AllPlayer(); update.FullName = fullname.Text.ToString(); update.Age = Convert.ToInt32(age.Text); update.Position = position.Text.ToString(); update.Country = country.Text.ToString(); //I went nuts on this as well, I know I have done it correctly but does I do not know how why or what did it but it did not like the way I had called the method //Crazy how one small issue takes such a long time to fix a issue. I was passing the parameters directly into the object. Took me ages to //to figure out that I had to assign the gridview updatemethod to the object update method for the values to be updated. AllPlayerAcessLayer allplayer = new AllPlayerAcessLayer(); GridView1.UpdateMethod = allplayer.UpdatePlayers(playerid, update.FullName, update.Age, update.Position, update.Country); binddata(); }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName.Equals("Insert")) { TextBox fullname = (TextBox)GridView1.FooterRow.FindControl("txtfullName1"); TextBox age = (TextBox)GridView1.FooterRow.FindControl("txtAge1"); TextBox position = (TextBox)GridView1.FooterRow.FindControl("txtPosition1"); TextBox country = (TextBox)GridView1.FooterRow.FindControl("txtCountry1"); //Allows user to insert but validation is problem. To validate I need to use my brain and now I am too exhausted to continue using my brain for another //day or two. Will continue working further on this. if (age.Text == null) { //verify if the age entered is text and number need to find control for the validation and enable is true //or validation through server side than client side } AllPlayer insert = new AllPlayer(); insert.FullName = fullname.Text.ToString(); insert.Age = Convert.ToInt32(age.Text); insert.Position = position.Text.ToString(); insert.Country = country.Text.ToString(); insert.PlayerID = Convert.ToInt32(Session["TeamID"]); AllPlayerAcessLayer allplayer = new AllPlayerAcessLayer(); ObjectDataSource1.InsertMethod = allplayer.InsertPlayers(insert.FullName, insert.Age, insert.Position, insert.Country, insert.PlayerID); binddata(); } }
private void binddata() { //I do not know why I was not able to bind the data after the insert so had to switch around some code as previously //I had all these code inside page load. AllPlayer player = new AllPlayer(); player.PlayerID = Convert.ToInt32(Session["TeamID"]); GridView1.DataSourceID = null; GridView1.DataSource = AllPlayerAcessLayer.GetAllPlayer(player.PlayerID); GridView1.DataBind(); }