Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am developing application in vb.net
aspx:
ASP.NET
<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
VB
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
Updated 4-Oct-12 21:08pm
v2
Comments
Sandeep Mewara 5-Oct-12 3:31am    
Did you Debug and see the execution flow?
Samsani.v.s.Durga Prasad 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

XML
<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";
        }
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900