Sunday, February 19, 2012

compiler is not recognizing my using statement for SglConnection statement

I am using ASP.NET 2.0, and am attempting to write some code to connect to the database and query a data table. The compiler is not recognizing my SqlConnection statement. It does recognize other commands. And just to make sure, I created other sql objects such as ObjectDataSource and SqlDataSource. The compiler does not find a problem with that code.

Basically the compiler is telling me that I am missing a "using" directive. The compiler is wrong though, because I am including the statement "usingSystemData" Can someone please take a look at my code below and to see if you notice what the problem might be? Note that I numbered the lines of code below. Note that I also tried putting lines 3 trhough 6 before line 2(The page directive) but that did not fix the problem The compiler still gives me the same compiler message.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request.Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0246: The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)Source Error:

Line 21: SqlConnection sqlConn = new SqlConnection("server=localhost;uid=sa;pwd=password;database=master;");

1 <asp:sqldatasource runat="server"></asp:sqldatasource>

2 <%@. Page Language="C#"%>

3 using System;

4 using System.Data;

5 using System.Collections;

6 using System.Data.SqlClient;

7

8 <script runat=server>

9

10 protected void Page_Load(object o, EventArgs e)

11 {

12 ObjectDataSource dsa; // This works no problems from the compiler here

13 SqlDataSource ds; // This works no problems from the compiler

14

15 if (IsPostBack)

16 {

17 if (AuthenticateUser(txtUsername.Text,txtPassword.Text))

18 {

19 instructions.Text = "Congratulations, your authenticated!";

20 instructions.ForeColor = System.Drawing.Color.Red;

21 SqlConnection sqlConn = new SqlConnection("server=localhost;uid=sa;pwd=password;database=master;");

22 String sqlStmt = "Select UserName from LogIn where UserName='" + txtUsername.Text + "' and password='" + sHashedPassword + "'";

23 }

24 else

25 {

26 instructions.Text = "Please try again!";

27 instructions.ForeColor = System.Drawing.Color.Red;

28 }

29 }

30

31 }

32

33 bool AuthenticateUser(string username, string password)

34 {

35 // Authentication code goes here

36

37 }

When using the in-line server code instead of code-behind, you have to use the following syntax instead:

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

No comments:

Post a Comment