Error using ASP.NET C#

B

Bart de Vries

Hello,

I new on developing with ASP.NET and i wrote some lines of code. This
works fine when all the code is in one page, but when i split it up into and
ASPX file and make an DLL using a namespace i get an error :

============= ERROR
Compiler Error Message: CS0234: The type or namespace name 'SQLGetPhones'
does not exist in the class or namespace 'WallpaperEditorDataManager' (are
you missing an assembly reference?)

Source Error:

Line 12: public void fnFillFormData()
Line 13: {
Line 14: PhoneList.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select * from phones").DefaultView;
Line 15: PhoneList.DataBind();
Line 16:

Source File: E:\wwwroot\aspx\test1.aspx Line: 14
=========== / ERROR

I used the following command to compile the DLL:

csc /t:library /r:System.dll /r:System.Data.dll
/out:bin/WallpaperEditorDataManager.dll test1.cs


Could someone explain me what is going wrong? I also,below, added the lines
of code i use.

Please help me with my first steps.

Bart de Vries



=========== SOURCE test1.cs
using System;
using System.Data;
using System.Data.SqlClient;


namespace WallpaperEditorDataManager
{
public class PhonesDB
{
public DataTable SQLGetPhones(string strQuery)
{

SqlConnection myConnection = new SqlConnection("User
ID=xxxx;password=xxxx;Initial Catalog=xxxx;Data Source=localhost;");
SqlDataAdapter myAdapter = new SqlDataAdapter(strQuery,
myConnection);
DataSet phones = new DataSet();
myAdapter.Fill(phones,"phones");

return phones.Tables[0];
}
}
}

========== / SOURCE test1.cs


========== SOURCE test1.aspx

<%@ Page Language="c#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="WallpaperEditorDataManager" %>
<script runat="server">

void Page_Load(Object s, EventArgs e)
{
fnFillFormData();
}

public void fnFillFormData()
{
PhoneList.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select * from phones").DefaultView;
PhoneList.DataBind();


ddlPhoneBrand.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select DISTINCT phone_brand from
phones").DefaultView;
ddlPhoneBrand.DataTextField = "phone_brand";
ddlPhoneBrand.DataValueField = "phone_brand";
ddlPhoneBrand.DataBind();
}

</script>


blah blah blah, some more lines containing the HTML and
ASP data elements

========== / SOURCE test1.aspx
 
B

Bart de Vries

is there a specific reason for using Codebehind instead of the other way?
And could you also still tell me why the first option is going wrong?

Bart de Vries
 
H

Hans Kesting

Bart,

You are trying to call an instance method (SQLGetPhones), which belongs
to the class PhonesDB, directly from the namespace. That is not possible.

two solutions:
a) first an instance of PhonesDb, then call the method
PhonesDB myDB = new PhonesDB();
PhoneList.DataSource = myDB.SQLGetPhones(...);

b) make it a static method and call that
add "static" to the declaration:
public static DataTable SQLGetPhones(string strQuery)
PhoneList.DataSource = PhonesDB.SQLGetPhones(...)

Hans Kesting

Bart de Vries said:
Hello,

I new on developing with ASP.NET and i wrote some lines of code. This
works fine when all the code is in one page, but when i split it up into and
ASPX file and make an DLL using a namespace i get an error :

============= ERROR
Compiler Error Message: CS0234: The type or namespace name 'SQLGetPhones'
does not exist in the class or namespace 'WallpaperEditorDataManager' (are
you missing an assembly reference?)

Source Error:

Line 12: public void fnFillFormData()
Line 13: {
Line 14: PhoneList.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select * from phones").DefaultView;
Line 15: PhoneList.DataBind();
Line 16:

Source File: E:\wwwroot\aspx\test1.aspx Line: 14
=========== / ERROR

I used the following command to compile the DLL:

csc /t:library /r:System.dll /r:System.Data.dll
/out:bin/WallpaperEditorDataManager.dll test1.cs


Could someone explain me what is going wrong? I also,below, added the lines
of code i use.

Please help me with my first steps.

Bart de Vries



=========== SOURCE test1.cs
using System;
using System.Data;
using System.Data.SqlClient;


namespace WallpaperEditorDataManager
{
public class PhonesDB
{
public DataTable SQLGetPhones(string strQuery)
{

SqlConnection myConnection = new SqlConnection("User
ID=xxxx;password=xxxx;Initial Catalog=xxxx;Data Source=localhost;");
SqlDataAdapter myAdapter = new SqlDataAdapter(strQuery,
myConnection);
DataSet phones = new DataSet();
myAdapter.Fill(phones,"phones");

return phones.Tables[0];
}
}
}

========== / SOURCE test1.cs


========== SOURCE test1.aspx

<%@ Page Language="c#" Debug="true" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="WallpaperEditorDataManager" %>
<script runat="server">

void Page_Load(Object s, EventArgs e)
{
fnFillFormData();
}

public void fnFillFormData()
{
PhoneList.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select * from phones").DefaultView;
PhoneList.DataBind();


ddlPhoneBrand.DataSource =
WallpaperEditorDataManager.SQLGetPhones("Select DISTINCT phone_brand from
phones").DefaultView;
ddlPhoneBrand.DataTextField = "phone_brand";
ddlPhoneBrand.DataValueField = "phone_brand";
ddlPhoneBrand.DataBind();
}

</script>


blah blah blah, some more lines containing the HTML and
ASP data elements

========== / SOURCE test1.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top