Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the "Using" statement in ASP.net razor webpages?

So I need to add a "using" statement which is :

using System.Data.SqlClient 

in my webpage so i dont have to call whats inside the SqlClient with the whole statement

 System.Data.SqlClient.SqlConnection con = new ..

How to do that ?

note that i added the reference System.Data to my webconfig file.

like image 374
user2962142 Avatar asked Feb 20 '16 14:02

user2962142


People also ask

How do you call a method in Razor pages?

Razor pages have handler-methods which are HTTP verbs. So to call a method from your page you need to put On followed by the http verb you want then your method name . And in your view pass the name to the asp-page-handler without the OnPost or OnGet prefix or Async suffix.

How can you comment using Razor syntax?

Comments Razor View Engine has two types of comments, one is single-line and another is multiline. Razor uses the syntax "@* .. *@" for the comment block but in a C# code block we can also use "/* */" or "//".

How can you comment using Razor syntax in MVC?

In visual studio, select some code/markup in your razor view and press Ctrl+K, Ctrl+C, and it'll comment the selection as described above.

What is @: In Cshtml?

This operator is useful in conjunction with other Razor server side operators when you want to output something as a literal text. For example: @if (model. Foo) { @:Some text to be written directly. }


1 Answers

At the top of your Razor View just add

@using System.Data.SqlClient 

Notice the @ char before the using statement.

Or inside a block like below:

@{
    using System.Data.SqlClient;
}

Side note: it's a little bit weird to use a SqlConnection into a Razor View.

like image 107
CodeNotFound Avatar answered Sep 28 '22 02:09

CodeNotFound