Link to home
Start Free TrialLog in
Avatar of jjrr007
jjrr007

asked on

Syntax Error in C# Web Form

I am working with a webform.  Using the C# code below in the snippet, I am generating the following syntax error:

Error      1      The type or namespace name 'GridViewRowEventArgs' could not be found (are you missing a using directive or an assembly reference?)      C\...Default.aspx.cs      1

I wanted to please ask for some assistance on resolving the syntax error and ensuring that it works.  This code should set a field to have a red background when a column's existing value is N/A.  This should occur when the GridView is being updated (not the new value).   The value of the color is in the textbox called TextBox3.  Also, the label is called Label4.  


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Windows.Forms;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void GridView1_RowUpdated(object sender, GridViewRowEventArgs e)
    {
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
            Label lbl = (Label)e.Row.FindControl("Label4");
 
            if ((string)DataBinder.Eval(e.Row.DataItem, "TextBox3") == "N/A")
            {
 
                lbl.BackColor = System.Drawing.Color.Red;
 
            }
        }
    }
 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
 
    }
}

Open in new window

Avatar of CuteBug
CuteBug
Flag of India image

You have to add a reference to the dll which contains the definition of GridViewRowEventArgs.
Avatar of jjrr007
jjrr007

ASKER

How do I do that?  I can try what you suggest and let you know how it works.
Try adding the following directive at the top:

using System.Web.UI.WebControls;

Open in new window

Avatar of jjrr007

ASKER

Thanks for your time. I tried that and generated the folloowing messages:

* The name 'DataBinder' does not exist in the current context
* 'Label' is an ambiguous reference between 'System.Windows.Forms.Label' and 'System.Web.UI.WebControls.Label'      
* Cannot convert type 'System.Web.UI.Control' to 'System.Windows.Forms.Label'      

What do you suggest that i change in the code?  

Avatar of jjrr007

ASKER

In case it is needed, I have posted the entire asp.net code below. The C# and the asp.net code below is all of the code used for the project.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
  <script language="javascript" type="text/javascript">
    function ToggleGridView()
    {
        var gridId = "<%=GridView1.ClientID%>";
        if(document.getElementById(gridId).style.visibility == "hidden")
        {
            document.getElementById(gridId).style.visibility="visible";
            document.getElementById(gridId).style.display="block";
        }
        else
        {
            document.getElementById(gridId).style.visibility="hidden";
            document.getElementById(gridId).style.display="block";
        
        }
            
    }
    </script>
 
<body>
    <form id="form1" runat="server">
        <ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
        <a href="javascript:ToggleGridView()" runat="server" id="toggler">Toggle</a>
        <div>
            &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
                SelectCommand="NewRickID" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        </div>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
            SelectCommand="NewRick" SelectCommandType="StoredProcedure" UpdateCommand="NewRickUpdate"
            UpdateCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="999" Name="ProductID" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="ProductNumber" Type="String" />
                <asp:Parameter Name="Color" Type="String" />
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="StartDate" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:TemplateField HeaderText="Color" SortExpression="Color">
                    <EditItemTemplate>
                        <asp:TextBox ID="textbox3" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StartDate" SortExpression="StartDate">
                
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                        
                        
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Calendar_scheduleHS.png" />
            <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1" PopupButtonID="Image1">
            </ajaxToolkit:CalendarExtender>
                        
                     
      
          
          
                    </EditItemTemplate>
                    
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("StartDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;&nbsp;&nbsp;
 
 
	
    </form>
</body>
</html>

Open in new window

For the DataBinder http://msdn.microsoft.com/en-us/library/system.web.ui.databinder.aspx you will need the following directive as well:

using System.Web.UI.DataBinder;

The second two errors are peculiar, I am not quite certain. You are creating a web application? Try removing the using System.Windows.Forms;
Avatar of jjrr007

ASKER

I made those changes and received the following message:

A using namespace directive can only be applied to namespaces; 'System.Web.UI.DataBinder' is a type not a namespace

I am not sure if I truly need the databinder in the c# code.  I am using a gridview to update the data that was created by a wizard. I really don't know what to change.  I am open to your suggestions.  Thanks again!
Sorry, that should have been:

using System.Web.UI;
Since this is a web form, you will need to have the following namespaces referenced. Normally they come by default. Not sure, how it is missing for you. Add all these and try ...

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
Avatar of jjrr007

ASKER

Thanks for your time. I didn't get any errors using these namespaces:

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

However, the code didn't work.  The code should code should set a field to have a red background when a column's existing value is N/A.  This should occur when the GridView is being updated (not the new value).  

I wanted to please ask what do you suggest.
Not sure if this is the problem, but instead of DataBinder, get textbox3 like you got the label

       if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
            Label lbl = (Label)e.Row.FindControl("Label4");
 
            Object o = e.Row.FindControl("TextBox3");
            if(o!= null)
            {
            if (((TextBox)o).Text == "N/A")
            {
 
                lbl.BackColor = System.Drawing.Color.Red;
 
            }
             }
        }
Avatar of jjrr007

ASKER

Thanks for your time.  I didn't receive any error messages.  Unfortuantely, it didn't work.  I really need this to work.  I would appreciate if you could please let me know what changes to make.  

I have posted the C# and the ASP.NET code below. This represents all of the code used in the project.  
If you want me to post the stored procedures used in the data sources I can post that too (they are very simple select/update statements).
//ASP.NET code
 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
  <script language="javascript" type="text/javascript">
    function ToggleGridView()
    {
        var gridId = "Table1";
              if(document.getElementById(gridId).style.visibility == "hidden")
        {
            document.getElementById(gridId).style.visibility="visible";
            document.getElementById(gridId).style.display="block";
        }
        else
        {
            document.getElementById(gridId).style.visibility="hidden";
            document.getElementById(gridId).style.display="block";
        
        }
            
    }
    </script>
 
<body>
    <form id="form1" runat="server">
        <ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" />
        <a href="javascript:ToggleGridView()" runat="server" id="toggler">Toggle</a>
        <div>
            &nbsp;<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
                SelectCommand="NewRickID" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
        </div>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksDWConnectionString %>"
            SelectCommand="NewRick" SelectCommandType="StoredProcedure" UpdateCommand="NewRickUpdate"
            UpdateCommandType="StoredProcedure">
            <SelectParameters>
                <asp:Parameter DefaultValue="999" Name="ProductID" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="ProductNumber" Type="String" />
                <asp:Parameter Name="Color" Type="String" />
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="StartDate" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:TemplateField HeaderText="Color" SortExpression="Color">
                    <EditItemTemplate>
                        <asp:TextBox ID="textbox3" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StartDate" SortExpression="StartDate">
                
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("StartDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <table style="width: 188px" id="Table1">
            <tr>
                <td>
                    Hello</td>
                <td>
                    Hello</td>
                <td>
                    Hello</td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                </td>
            </tr>
            <tr>
                <td>
                </td>
                <td>
                </td>
                <td>
                </td>
            </tr>
        </table>
 
 
	
    </form>
</body>
</html>
 
 
//C#.NET Code is below
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
 
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
 
 
    protected void GridView1_RowUpdated(object sender, GridViewRowEventArgs e)
    {
 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
 
            Label lbl = (Label)e.Row.FindControl("Label4");
 
            Object o = e.Row.FindControl("TextBox3");
            if (o != null)
            {
                if (((TextBox)o).Text == "N/A")
                {
 
                    lbl.BackColor = System.Drawing.Color.Red;
 
                }
            }
        }
 
 
    }
 
}
   

Open in new window

I am attaching a working code.. for a simple scenario. You can extend it further.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default7.aspx.cs" Inherits="Default7" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
  <script language="javascript" type="text/javascript">
    function ToggleGridView()
    {
        var gridId = "Table1";
              if(document.getElementById(gridId).style.visibility == "hidden")
        {
            document.getElementById(gridId).style.visibility="visible";
            document.getElementById(gridId).style.display="block";
        }
        else
        {
            document.getElementById(gridId).style.visibility="hidden";
            document.getElementById(gridId).style.display="block";
        
        }
            
    }
    </script>
 
<body>
    <form id="form1" runat="server">
        <a href="javascript:ToggleGridView()" runat="server" id="toggler">Toggle</a>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="Data Source=HEMAAJITH\SQLEXPRESS;Initial Catalog=practice;Integrated Security=SSPI"
            SelectCommand="Select * from Product where productid = @ProductID" 
            UpdateCommand="Update Product set [Name]=@name, [ProductNumber]=@ProductNumber,Color=@Color,StartDate=@StartDate where ProductID=@ProductID">
            <SelectParameters>
                <asp:Parameter DefaultValue="1" Name="ProductID" Type="String" />
            </SelectParameters>
            <UpdateParameters>
                <asp:Parameter Name="Name" Type="String" />
                <asp:Parameter Name="ProductNumber" Type="String" />
                <asp:Parameter Name="Color" Type="String" />
                <asp:Parameter Name="ProductID" Type="Int32" />
                <asp:Parameter Name="StartDate" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowUpdating="GridView1_RowUpdating" OnRowDataBound="GridView1_RowDataBound">
            <Columns>
                <asp:CommandField ShowEditButton="True" />
                <asp:TemplateField HeaderText="ProductID" SortExpression="ProductID">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label3" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:TemplateField HeaderText="Color" SortExpression="Color">
                    <ItemTemplate>
                        <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox ID="textbox3" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="StartDate" SortExpression="StartDate">
                
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("StartDate") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 
 
 
	
    </form>
</body>
</html>
 
 
Code Behind:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
 
 
public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.FindControl("Label4") != null)
        {
            Label lbl = (Label)e.Row.FindControl("Label4");
            if(highlightColorCol)
                lbl.BackColor = System.Drawing.Color.Red;
        }
    }
 
    private bool highlightColorCol = false;
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        if(e.NewValues[2].ToString() == "N/A")
        {
            highlightColorCol = true;
 
        }
    }
 
}

Open in new window

Avatar of jjrr007

ASKER

First off, thanks for your time.  I appreciate that!  I apologize that this question keeps going.

When I ran the code and placed a new value of N/A in the GridView it turned red.  However, I mentioned in the question that the background of the cell should turn red when the existing value is N/A

When the user clicks edit on a row that has an existing value of "N/A" in the color column, the background of the cell should turn red.  The red should only appear when the existing value in the color column is N/A- after the edit button is clicked.  
ASKER CERTIFIED SOLUTION
Avatar of ajitha75
ajitha75
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jjrr007

ASKER

It worked:) Thanks a lot!

 I never thought that such a simple thing would be so complex with C#.  I learned a lot too.  Thanks again.
Avatar of jjrr007

ASKER

I found that the code below, can look for a N/A value in textbox 4 and give it a red- read-only value.  Also, it can  make all values in textbox2 red/read only regardless of the value
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls.Adapters;
using System.Web.UI.WebControls.WebParts;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
 
 
public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        
 
        if (e.Row.FindControl("TextBox4") != null)
        {
            TextBox lbl = (TextBox)e.Row.FindControl("TextBox4");
            if (oldIsNA)
            {
                lbl.BackColor = System.Drawing.Color.Red;
                lbl.Attributes.Add("readonly", "readonly");
 
            }
                    
 
        }
 
        if (e.Row.FindControl("TextBox2") != null)
        {
            TextBox lbl2 = (TextBox)e.Row.FindControl("TextBox2");
            if (sourceReadOnly)
            {
                lbl2.BackColor = System.Drawing.Color.Red;
                lbl2.Attributes.Add("readonly", "readonly");
 
            }
 
 
        }
 
 
 
    }
 
 
 
    private bool oldIsNA = false;
    private bool sourceReadOnly = false;
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.NewEditIndex];
        if (row != null)
        {
            if(row.FindControl("Label4") != null)
            {
                Label lbl = (Label)row.FindControl("Label4");
                if (lbl.Text == "N/A")
                    oldIsNA = true;
            }
 
            if (row.FindControl("Label2") != null)
            {
                Label lbl = (Label)row.FindControl("Label2");
                sourceReadOnly = true;
            }
        }
 
 
 
        
    }
 
 
}

Open in new window