Link to home
Start Free TrialLog in
Avatar of HenningF
HenningF

asked on

ASP.NET rookieQ: 'MyDataGrid is not declared'

I have the following in the top of one of my .aspx-files:

---------------------------------------------------------
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<SCRIPT LANGUAGE="VB" RUNAT=server>
  Sub Page_Load(Sender As Object, E As EventArgs)
      Dim DS As DataSet
      Dim MyConnection As SqlConnection
      Dim MyCommand As SqlDataAdapter
      MyConnection = New SqlConnection("connection")
      MyCommand = New SqlDataAdapter("SELECT * FROM table", MyConnection)

      DS = new DataSet()
      MyCommand.Fill(ds, "kapittel")
      MyDataGrid.DataSource=ds.Tables("table").DefaultView
      Page.DataBind()
  End Sub
</SCRIPT>
---------------------------------------------------------

When I run this I get the error-message "BC30451: Name 'MyDataGrid' is not declared." and I have no idea how to solve it. This code is almost exactly the same as downloaded from the asp.net-website. I have tried to use Dim MyDataGrid AS ... but I haven't found the right arguments. The exact same code also works in one of my ascx-files...go figure...

HenningF


Avatar of cdpMat
cdpMat

You just need to rename your grid to 'MyDataGrid'. It's probably named DataGrid1 at the moment. Click it and select Properties to view it's name.
Avatar of HenningF

ASKER

OK, so what you are saying is that 'MyDataGrid' is some kind of reserved word? What if I want to have several different datagrids within the same page...?

My exact code at the moment looks like this:
---------------------------------------------------------
  Sub Page_Load(Sender As Object, E As EventArgs)
    Dim DS1 As DataSet
    Dim DS2 As DataSet
    Dim MyConnection As SqlConnection
    Dim MyCommand1 As SqlDataAdapter
    Dim MyCommand2 As SqlDataAdapter

    MyConnection = New SqlConnection("server=something, ...")
    MyCommand1 = New SqlDataAdapter("SELECT * FROM table1", MyConnection)
    MyCommand2 = New SqlDataAdapter("SELECT * FROM table2", MyConnection)

    DS1 = new DataSet()
    DS2 = new DataSet()
    MyCommand1.Fill(ds1, "table1")
    MyCommand2.Fill(ds2, "table1")

    MyDataGrid1.DataSource=ds1.Tables("table1").DefaultView
    MyDataGrid2.DataSource=ds2.Tables("table1").DefaultView
    Page.DataBind()
  End Sub
---------------------------------------------------------
...a small typo in the 3rd line from the bottom, it should be "MyDataGrid2.DataSource=ds2.Tables("table2").DefaultView"
No, I'm not saying that. I'm saying that your code was referring to a DataGrid which did not exist so was causing an error.

You have changed the code since you first asked the question - are you still having problems?
It's actually the last code I'm using...I just modified it to keep you from reading all that much. It still doesn't work...
ASKER CERTIFIED SOLUTION
Avatar of cdpMat
cdpMat

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
what cdpmat said, is correct....
To make sure...show us where have you defined your variables MyDataGrid1, MyDataGrid2, ......etc.

do you have a line of code in your application that reads like..

Dim MyDataGrid1 As DataGrid

Or in ascx file do you have

<asp:DataGrid Runat=server ID="MyDataGrid1"></asp:DataGrid>
I have a

<ASP:DROPDOWNLIST AUTOPOSTBACK="True" ID="MyDataGrid1" DATAVALUEFIELD="KapittelID" DATATEXTFIELD="Kapittelnavn" />

later on in my page (I don't use code-behind in this page). So maybe if I put something like Dim MyDataGrid1 As Dropdownlist it might work? I apologise for all this but I'm a total rookie, and I'm beginning to wonder if I should take a class on this instead of trying myself...
A DropDownList control is totally different to a DataGrid control. Delete the dropdown and replace it with a DataGrid control. Make sure you name it MyDataGrid1.
OK, it seems like I misunderstood this part, and I'm now looking more into how to make dropdownlists. I somehow got the idea that a DataGrid was a kind of "universal datastorage" and that I could use it in whatever manner I wanted. I see now that this is not the case, and you cdpMat will offcourse recieve the points for guiding me in the right direction.

Thanx,
HenningF
A DataGrid is useful for displaying a table of data on a web page. You're probably thinking of a DataSet which temporarily holds tables of data to be used in controls. You can easily bind data to a drop down list control but you need to specify fields for the value and text etc.