Iam not getting values I entered in the gridview for updation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sailaja71185
    New Member
    • Nov 2009
    • 2

    Iam not getting values I entered in the gridview for updation

    Here is my code:

    Code:
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using HomeDataLayer;
    using System.Windows.Forms;
    public partial class Reminders : System.Web.UI.Page
    {
    
    
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                txtRemindDate .Text  = System.DateTime.Now.Date.ToShortDateString();
                FillGrid();
            }
        }
    
        public void FillGrid()
        {
            DataTable dt = HomeManagement.SelectReminders();
            if (dt != null)
                if (dt.Rows.Count > 0)
                {
                    RemindersGrid.DataSource = dt;
                    RemindersGrid.DataBind();
                    for (int i = 0; i < RemindersGrid.Rows.Count; i++)
                    {
                        if (RemindersGrid.Rows[i].Cells[4].Text == Convert.ToString(System.DateTime.Now.Date))
                        {
                            RemindersGrid.Rows[i].BackColor = System.Drawing.Color.LightYellow;
                            //string msg = RemindersGrid.Rows[i].Cells[1].Text;
                            //msg += "<br>" + RemindersGrid.Rows[i].Cells[4].Text;
                            //System.Windows.Forms.MessageBox.Show(msg);
                        }
                    }
    
                }
                else
                {
                    RemindersGrid.DataSource = null;
                    RemindersGrid.DataBind();
                }
        }
    
        
        protected void submit_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                int i = HomeManagement.InsertReminder(txtIssue.Text, txtName.Text, Convert.ToDateTime(txtRemindDate.Text), System.DateTime.Now.Date );
                if (i > 0)
                {
                    System.Windows.Forms.MessageBox.Show("Succcessfully Inserted");
                     txtRemindDate .Text  = System.DateTime.Now.Date.ToShortDateString();
                    txtIssue.Text = txtName.Text = "";
    
                }
            }
            catch (Exception err)
            {
                System.Windows.Forms.MessageBox.Show(err.ToString ());
            }
           
        }
        protected void RemindersGrid_RowEditing(object sender, GridViewEditEventArgs e)
        {
            RemindersGrid.EditIndex = e.NewEditIndex;
            FillGrid();
        }
        protected void RemindersGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            bool flag;
            if (RemindersGrid.DataSource == null)
                flag = false;
            else
            {
           
            }
            try
            {
                RemindersGrid.EditIndex = e.RowIndex;
                int ID = e.RowIndex;
                string name = RemindersGrid.Rows[e.RowIndex ].Cells[2].Text ;
                int i = HomeManagement.UpdateReminder(RemindersGrid.Rows[e.RowIndex].Cells[5].Text, RemindersGrid.Rows[ID].Cells[2].Text, Convert.ToDateTime(RemindersGrid.Rows[ID].Cells[4].Text), Convert.ToDateTime(RemindersGrid.Rows[ID].Cells[3].Text), int.Parse(RemindersGrid.Rows[ID ].Cells[1].Text));
                if (i > 0)
                    MessageBox.Show("Update Successfully");
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString());
            }
    
    
        }
        protected void RemindersGrid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int index = e.RowIndex;
            FillGrid();
           RemindersGrid.EditIndex = e.RowIndex;
    
           int i = HomeManagement.DeleteReminders(int.Parse(RemindersGrid.Rows[index].Cells[1].Text));
           if (i > 0)
               MessageBox.Show("Deleted Successfully");
    
        }
        protected void RemindersGrid_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
           // FillGrid();
            RemindersGrid.EditIndex = -1;
          
        }
    }
    Can any one solve my problem?
    Last edited by Frinavale; Nov 20 '09, 04:42 PM. Reason: Please post code in [code] .. [/code] tags. Added code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Could you please post the ASP code for the GridView...I can't get a clear idea of what you are trying to do unless I see it's markup.

    Aside from that, and completely unrelated to the problem, You shouldn't be using System.Windows. Forms.MessageBo x in an ASP.NET application. Also, you should look into using the GridView.RowDat aBind Event to set the background colour instead of doing this in your FillGrid method.

    -Frinny

    Comment

    • sailaja71185
      New Member
      • Nov 2009
      • 2

      #3
      Hi thanks for Ur reply, here is my design code:
      Code:
                     <asp:Panel ID="DataPanel"  runat ="server" BorderColor ="green" BorderWidth ="0px" width ="900px">
                <table style="width: 883px" >
                <tr>
               
                
                <td >
                  <asp:Label ID="lblRemindDate" runat="server" Font-Names="Tahoma" Font-Size="12px" 
                          Text="RemindDate: "></asp:Label>
                    </td>
                    <td>
                    <asp:TextBox ID="txtRemindDate" runat="server" Font-Names="Tahoma" 
                          Font-Size="12px" TabIndex="40" BackColor="AliceBlue"></asp:TextBox>
                               <cc1:CalendarExtender ID="txtRemindDate_CalendarExtender" 
                                               runat="server" CssClass="MyCalendar" Enabled="True" Format="MM/dd/yyyy" 
                                               PopupButtonID="ImageButtonCalendarPopupRemindDate" 
                                               TargetControlID="txtRemindDate">
                                           </cc1:CalendarExtender>
                                           <cc1:MaskedEditExtender ID="txtRemindDate_MaskedEditExtender" 
                                               runat="server" ClearMaskOnLostFocus="False" CultureAMPMPlaceholder="" 
                                               CultureCurrencySymbolPlaceholder="" CultureDateFormat="" 
                                               CultureDatePlaceholder="" CultureDecimalPlaceholder="" 
                                               CultureThousandsPlaceholder="" CultureTimePlaceholder="" Enabled="True" 
                                               Mask="99/99/9999" TargetControlID="txtRemindDate">
                                           </cc1:MaskedEditExtender>
                                            <asp:ImageButton ID="ImageButtonCalendarPopupRemindDate" runat="Server" 
                                              AlternateText="Click to show calendar" CausesValidation="false" 
                       ImageUrl="~/Resources/images/Calendar_scheduleHS.png" TabIndex="135" />
                          
                        </td> 
                        </tr> 
                        <tr>
                        <td >
                            <asp:Label ID="lblName" runat="server" Font-Names="Tahoma" Font-Size="12px" Text="PersonName: "></asp:Label>
                   </td> 
                   <td>
                   <asp:TextBox ID="txtName" runat="server" Font-Names="Tahoma" 
                          Font-Size="12px"
                          TabIndex="40" BackColor="AliceBlue"></asp:TextBox>
                          </td> 
                          </tr> 
                          <tr><td >
                          <asp:Label ID="lblIssue" runat="server" Font-Names="Tahoma" Font-Size="12px" 
                              Text="Issue: "></asp:Label></td>
                              <td>
                    <asp:TextBox ID="txtIssue" runat="server" Font-Names="Tahoma"  TextMode ="MultiLine" 
                          Font-Size="12px"
                          TabIndex="40" BackColor="AliceBlue" style="margin-left: 4px" Width="271px"></asp:TextBox>
                   </td> 
                   </tr> 
                   <tr><td class="style1">
                   
                   
                      <asp:ImageButton ID="submit" runat ="server" 
                          ImageUrl ="~/Resources/images/submit_button.gif" AlternateText ="Add Reminder"
                       onclick="submit_Click" />
                   </td> 
                   </tr>
                   </table>
                   
                       </asp:Panel> 
      
      
                <asp:Panel ID="GridPanel" runat ="server" style="z-index :105;" BorderColor ="green" BorderWidth ="0px" width ="900px">
                 
                <asp:Label ID="ExistingReminders" Font-Bold ="true" Font-Names ="Tahoma" Font-Size ="13px" runat ="server" 
       Text ="Existing Reminders" ></asp:Label> 
       <asp:GridView ID="RemindersGrid"  runat ="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
                                AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
                                onrowdeleting="RemindersGrid_RowDeleting" 
                                onrowediting="RemindersGrid_RowEditing" 
                                onrowupdating="RemindersGrid_RowUpdating" 
                                onrowcancelingedit="RemindersGrid_RowCancelingEdit"  > 
      
           <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <RowStyle BackColor="#F7F6F3" ForeColor="#333333"  Font-Size ="12px" Font-Names ="Tahoma"/>
           <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
           <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
           <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
           <EditRowStyle BackColor="#999999" />
           <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
           
        
        </asp:GridView>

      I've no problem with message box.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I think I might know why you're having a problem.
        You have no EditTemplate specified for your columns.

        Your GridView should look something like:
        Code:
        <asp:GridView ID="RemindersGrid"  runat ="server" CellPadding="4"
              ForeColor="#333333" GridLines="None" 
              AutoGenerateDeleteButton="True" AutoGenerateEditButton="True" 
              AutoGenerateColumns="False"
        
                    onrowdeleting="RemindersGrid_RowDeleting" 
                    onrowediting="RemindersGrid_RowEditing" 
                    onrowupdating="RemindersGrid_RowUpdating" 
                    onrowcancelingedit="RemindersGrid_RowCancelingEdit"  > 
           
          <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <RowStyle BackColor="#F7F6F3" ForeColor="#333333"  Font-Size ="12px" Font-Names ="Tahoma"/>
          <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
          <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
          <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
          <EditRowStyle BackColor="#999999" />
          <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        
          <Columns>
            <asp:TemplateField headertext="Column1">
              <ItemTemplate>
                <asp:Label id="NameLabel" Text= '<%# Eval("name") %>' runat="server"/>         
              </ItemTemplate>
              <EditTemplate>
                <asp:TextBox id="NameTextBox" Text= '<%# Eval("name") %>' runat="server"/>    
              </EditTemplate>
            </asp:TemplateField>
          </Columns>
         </asp:GridView>
        Notice how I have added a TemplateField to the Columns section of the GridView?

        This TemplateField tells the GridView how to display the data that it's bound to.

        In this case the TemplateField specifies that the data in the column "name" should be displayed in a Label if the user is just viewing the GridView. It also specifies that the data in column "name" should be displayed in a TextBox if the user is editing the GridView.

        Now you can let the user edit the data.
        You can get the values the user entered using the GridViewRow.Fin dControl method when the user is updating.

        (Notice that I set the GridView's AutoGenerateCol umns property to False. If you don't do this then 2 columns will appear for the "name" column: one automatically generated, and the other displayed in the TemplateField)


        For example:
        Code:
        protected void RemindersGrid_RowUpdating(object sender, GridViewUpdateEventArgs e)
             {
                 bool flag;
                 if (RemindersGrid.DataSource == null)
                     flag = false;
                 else
                 {
          
                 }
                 try
                 {
                     RemindersGrid.EditIndex = e.RowIndex;
                     int ID = e.RowIndex;
                     TextBox nameTextBox = (TextBox) RemindersGrid.Rows[e.RowIndex ].FindControl("NameTextBox");
                     string name = nameTextBox.Text ;
        //...............................................
                 }
                 catch (Exception err)
                 {
                     MessageBox.Show(err.ToString());
                 }
             }
        Now, regarding the MessageBox, it will work ok in Internet Explorer but it will not work in any other browser. The MessageBox is a Windows.Forms control not a Windows.Web control.

        You should not be using Windows.Forms controls in a web application.

        Even if the end user is displayed the MessageBox it's not a good idea.
        The reason is because an ActiveX control would be required in order to display the MessageBox. This means that your website is restricted to being viewed only in Internet Explorer. Also, ActiveX controls are pron to security flaws that may open the end user computer up to vulnerabilities . ActiveX controls are not trusted and many users have them turned off...which means that the end user is unlikely to be able to see the message box.


        I recommend placing a Label on the page and setting it's Text to any error messages instead of using the Windows.Forms.M essageBox control.

        -Frinny

        Comment

        Working...