Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
i am developing application in vb.net
aspx:
<div>
<asp:gridview runat="server" id="grid1">
<asp:bound field......./>--empid
<asp:bound field......./>--empname
<asp:bound field......./>--deptname
 
</asp:gridview>
<asp:lable runat="server" id="lblerror" />
</div>
 
axpx.cs:
 

i have a grid inside i have 3 bound columns
1.empid
2.empno(editable)
3.empname
 
empno should allow only 20,21 ,if i enter not 20,21 then i want to show error message
dim x as boolean=false
 
so in row updating event
public sub gridview1_rowupdatingevent(s.........,e....)handles .......
{
if(empno=20 OR empno=21) then
 
//implementation
x=true
END IF
if(x=false)then
 
lblmessage.text="please enter 20,21."
END IF
 
}
LABEL IS DISPLAYING AT PAGE_LOAD EVENT BUT SAME CONTROL IF I USE INSIDE ROW_UPDATING EVENT IT IS NOT WORKING. CAN YOU PLEASE GUIDE ME.
 
problem is lblmessage is was not displaying the text in runtime. please help me
Posted 4-Oct-12 16:42pm
Edited 4-Oct-12 22:08pm
v2
Comments
Sandeep Mewara at 5-Oct-12 3:31am
   
Did you Debug and see the execution flow?
Samsani.v.s.Durgapasad at 5-Oct-12 7:44am
   
simply write
 
if(empno==20 || empno==21)
{
Update logic
}
else
{
lblError.Text="Error occurred enter only 20 or 21"
}
 
This will be enough observe the solutoin1 gridviewupdating event logic
 

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

<div>
 
    <asp:GridView runat="server" ID="gridEmp" DataKeyNames="empid" AutoGenerateColumns="false" OnRowEditing="GridviewEmpEditing" OnRowUpdating="GridviewEmpUpdating" OnRowCancelingEdit="GridviewEmpCanceling" >
 
      <Columns>
 
        <asp:TemplateField HeaderText="Edit">
 
           <ItemTemplate>
            <asp:LinkButton runat="server" ID="linkEdit" Text="Edit" CommandName="Edit"></asp:LinkButton>
           </ItemTemplate>
           <EditItemTemplate>
             <asp:LinkButton runat="server" ID="linkUpdate" Text="Update" CommandName="Update"> </asp:LinkButton>
             <asp:LinkButton runat="server" ID="linkCancel" Text="Cancel" CommandName="Cancel"> </asp:LinkButton>
 
           </EditItemTemplate>
 
        </asp:TemplateField >
 

         <asp:TemplateField HeaderText="EmpId">
 
            <ItemTemplate>
                <%# Eval("empid")%>
            </ItemTemplate>
            <EditItemTemplate>
               <asp:TextBox runat="server" ID="txtEmpid" Text='<%# Eval("empid")%>'></asp:TextBox>
            </EditItemTemplate>
 
         </asp:TemplateField>
 
         <asp:TemplateField   HeaderText="EmpNo">
 
            <ItemTemplate>
                <%# Eval("empno")%>
            </ItemTemplate>
             <EditItemTemplate>
              <asp:TextBox runat="server" ID="txtEmpNo" Text='<%# Eval("empno")%>'></asp:TextBox>
            </EditItemTemplate>
         </asp:TemplateField>
 
         <asp:TemplateField HeaderText="EmpName">
 
            <ItemTemplate>
               <%# Eval("empname")%>
            </ItemTemplate>
            <EditItemTemplate>
             <asp:TextBox runat="server" ID="txtEmpName"  Text='<%# Eval("empname")%>'></asp:TextBox>
            </EditItemTemplate>
 
         </asp:TemplateField>
 
      </Columns>
 

 

    </asp:GridView>
 
      <asp:Label runat="server" ID="lblError"></asp:Label>
 
   </div>
 
In the griviewupdating event while you are updating the empno check whether entered empno is 20 or 21 like below then write the code for updating
 
int Empid = int.Parse(gridEmp.DataKeys[e.RowIndex].Value.ToString());
 
        GridViewRow row = gridEmp.Rows[e.RowIndex] as GridViewRow;
 
        TextBox txtEno = row.FindControl("txtEmpNo") as TextBox;
        TextBox txtEname = row.FindControl("txtEmpName") as TextBox;
 
        if (txtEno.Text == "20" || txtEno.Text == "21")
        {
 
            string updateCommand = "update empcode set empno=@empno, empname=@empname where empid=@empid";
 

            SqlConnection con = new SqlConnection(Connstr);
            {
 
                SqlCommand cmd = new SqlCommand(updateCommand, con);
                {
                    cmd.Parameters.AddWithValue("@empid", Empid);
                    cmd.Parameters.AddWithValue("@empno", txtEno.Text);
                    cmd.Parameters.AddWithValue("@empname", txtEname.Text);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
 
            }
 
            gridEmp.EditIndex = -1;
            this.GetData();
 
        }
     
        else
        {
            lblError.Text = "Error occured please enter 20 or 21";
        }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 360
1 Mathew Soji 309
2 DamithSL 225
3 Afzaal Ahmad Zeeshan 202
4 Maciej Los 190
0 OriginalGriff 6,249
1 Sergey Alexandrovich Kryukov 5,853
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 3,865


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 5 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100