0

so this time, I want to update a data but when I do, the table ends up becoming blank and nothing pops up. So for example, my modal will pop up and i can edit, but the fields end up becoming blank and when i try to click save changes, the table of the data in the gridview ends up blank. Can you see what i'm missing?

UPDATE MODAL

    <div id="UpdateModal" class="modal fade" role="dialog">
      <div class="modal-dialog">
        <!-- Modal content-->
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h4 class="modal-title">Update User Account</h4>
          </div>
          <div class="modal-body">
              Id No.: <asp:Label ID="lblGuestID" runat="server" Text=""></asp:Label>
                <table>
                    <tr>
                        <td><asp:Label ID="Label1" runat="server" Text="First Name"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdateFName" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                     <tr>
                        <td><asp:Label ID="Label2" runat="server" Text="Last Name"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdateLName" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                     <tr>
                        <td><asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label></td>
                        <td><asp:TextBox ID="txtUpdatePhone" runat="server" CssClass="form-control"></asp:TextBox> </td>
                    </tr>
                </table>
          </div>
          <div class="modal-footer">
              <asp:Button ID="btnExecuteUpdate" class="btn btn-default" runat="server" Text="Save" OnClick="btnExecuteUpdate_Click"/>
              <asp:Button ID="btnCancelUpdate" class="btn btn-default" runat="server" Text="Cancel" data-dismiss="modal"/>
          </div>
        </div>
      </div>
    </div>

GRIDVIEW

<asp:GridView ID="gvGuest" runat="server" AutoGenerateColumns="False"
        OnPageIndexChanging="PageIndexChanging" AllowPaging="true" PageSize="5"
        DataKeyNames="GuestID">
        <Columns>

             <asp:TemplateField HeaderText="Guest ID" InsertVisible="False" SortExpression="GuestID">
                <ItemTemplate>
                    <asp:Label ID="lblGuestID" runat="server" Text='<%#Eval("GuestID")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="First Name">
                <ItemTemplate>
                    <asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="Last Name">
                <ItemTemplate>
                    <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

             <asp:TemplateField HeaderText="Phone">
                <ItemTemplate>
                    <asp:Label ID="lblPhone" runat="server" Text='<%#Eval("Phone")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
             <asp:TemplateField HeaderText ="Actions">
                <ItemTemplate>
                    <asp:Button ID="btnUpdate" runat="server" ForeColor="Blue" Text="Update" OnClick="btnUpdate_Click"></asp:Button>
                    <asp:Button ID="btnDelete" runat="server" ForeColor="Red" Text="Delete" OnClick="btnDelete_Click"></asp:Button>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

BEL

 public class GuestBEL
    {
        public string GuestID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Phone { get; set; }
    }

BLL

public void UpdateGuest(GuestBEL objBEL)
        {
            GuestDAL objDAL = new GuestDAL();
            objDAL.UpdateGuest(objBEL);
        }

DAL

 public void UpdateGuest(GuestBEL objBEL)
        {
            SqlCommand cmd = new SqlCommand("GuestUpdate", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@FirstName", objBEL.FirstName);
            cmd.Parameters.AddWithValue("@LastName", objBEL.LastName);
            cmd.Parameters.AddWithValue("@Phone", objBEL.Phone);
            cmd.Parameters.AddWithValue("@GuestID", objBEL.GuestID);
            con.Open();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }

CS

 protected void btnUpdate_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "UpdateModal", "$('#UpdateModal').modal();", true);
            clickedRow = ((Button)sender).NamingContainer as GridViewRow;
            string pk = ((Label)clickedRow.FindControl("lblGuestID")).Text;
            DataTable dt = new DataTable();
            lblGuestID.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label1")).Text;
            Label1.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label2")).Text;
            Label2.Text = pk.ToString();
            pk = ((Label)clickedRow.FindControl("Label3")).Text;
            Label3.Text = pk.ToString();

        }

        protected void btnExecuteUpdate_Click(object sender, EventArgs e)
        {
            objBEL.GuestID = lblGuestID.Text;
            objBEL.FirstName = txtUpdateFName.Text;
            objBEL.LastName = txtUpdateLName.Text;
            objBEL.Phone = txtUpdatePhone.Text;
            objBLL.UpdateGuest(objBEL);
            gvGuest.EditIndex = -1;
            readGrid();
            objBEL = null;
            objBLL = null;
            objBEL = null;
            objBLL = null;
            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myPrompt", "alert('Successfully Saved Changes!');", true);
        }

Stored Procedure

CREATE PROCEDURE [dbo].[GuestUpdate]
    @GuestID int,
    @FirstName varchar(20),
    @LastName varchar(20),
    @Phone varchar(15)
AS
BEGIN
    UPDATE Guest SET FirstName = @FirstName, LastName = @LastName, Phone = @Phone
    WHERE GuestID = @GuestID
RETURN
END

READGRID

 private void readGrid()
        {
            DataSet data = new DataSet();
            data = objBLL.ReadGuest();
            gvGuest.DataSource = data;
            gvGuest.DataBind();
        }

INSERT

 protected void btnInsert_Click(object sender, EventArgs e)
        {
            objBEL.FirstName = txtFName.Text.Trim();
            objBEL.LastName = txtLName.Text.Trim();
            objBEL.Phone = txtPhone.Text.Trim();
            objBLL.SaveGuest(objBEL);
            ScriptManager.RegisterClientScriptBlock(btnSave, GetType(), "a", "alert('Guest details saved.')", true);
            txtFName.Text = string.Empty;
            txtLName.Text = string.Empty;
            txtPhone.Text = string.Empty;
            readGrid();
            objBEL = null;
            objBLL = null;
        }
Jed
  • 11
  • 3
  • Where is your readGrid method? – Ranjith May 14 '19 at 18:11
  • @Jed Code doesn't belong in comments. Please provide all code in the question necessary to produce a [MCVE]. – mason May 14 '19 at 18:19
  • @mason Oops, my bad. think you can help me find out what's wrong with my code? I really need all the help i can get – Jed May 14 '19 at 18:33
  • Is it pushing null or empty values to the database? or just not showing the data in the grid view after an update? – dvo May 14 '19 at 18:57
  • @dvo The data just doesn't show in the gridview. – Jed May 14 '19 at 19:08
  • Oh nice!! now the thing can update hehe. But now there's another problem, if I want to update, the data in the tables aren't showing in the modal. Do I just rearrange the code? – Jed May 14 '19 at 19:12
  • Where is the code where you fill your gridview? `ReadGrid` seems like it only pulls back data for one guest. – dvo May 14 '19 at 19:12
  • @dvo I just added the insert, but i think i should reword my previous statement. So for example, when i click the update button, the modal should show the data contained in the thing i'm editing, in which case the firstname, lastname, phone. But what I get back instead is blank. – Jed May 14 '19 at 19:19
  • @Jed you should have a method that will take the selected row and set the text property of the textboxes in your modal using the values of that grid view row. Then the user makes their changes and then updates the record. – dvo May 14 '19 at 19:21
  • Hmmmm, how would I be able to accomplish that @dvo? – Jed May 14 '19 at 19:24
  • @jed, you're close in `btnUpdate_Click`. You need to grab the individual row, grab the value from the cell that matches one of the textboxes, then set `textbox.Text = label.Text` – dvo May 14 '19 at 19:27
  • @dvo, hmm okay I think I get what you mean. I just want to check if i'm doing it right, so will adding a label button inside the update modal work? – Jed May 14 '19 at 19:36
  • @Jed, you do it with these two lines: `string pk = ((Label)clickedRow.FindControl("lblGuestID")).Text; lblGuestID.Text = pk.ToString();`. Do this, but with your text boxes and other labels. – dvo May 14 '19 at 19:38
  • Okay, so I got an error System.NullReferenceException: 'Object reference not set to an instance of an object.' @dvo. What did I do wrong? – Jed May 14 '19 at 19:49
  • @dvo, I've updated the CS and the Update Modal above. Can you see what's wrong with it? – Jed May 14 '19 at 19:52
  • @Jed, you're selecting the Labels from your GridView row. So you are finding the controls by ID that you set up in your grid view! Ex: `string fn = ((Label)clickedRow.FindControl("lblFirstName")).Text;`. Then you set your Textbox in your modal to that value: `txtUpdateFName.Text = fn;` – dvo May 14 '19 at 19:56
  • 1
    @dvo OMG IT WORKS!! HAHA,excuse my simple mindedness there, I didn't understand what you meant clearly haha. Thanks for the help my dude! :) – Jed May 14 '19 at 20:02

0 Answers0