0

Hi, i am new in asp.net and i found it quiet difficult... i was programming in classic asp and sql server, but asp.net looks quiet different, specially if i do program in C# ....

Than i hase a couple of questions :

1) if i do a form with 2 textboxes and i want to check if the value of the 2 textboxes are matching with the 2 records of my database (sql server), ho can i pass this 2 value??

2) if i don't find any record in my database.. how can i check this?

3) ho can i put the value of my text box in a cookies and the value of my database in a session?

Thanks for your help

3
Contributors
9
Replies
54
Views
3 Years
Discussion Span
Last Post by grafic.web
0

Compare with textbox value with Database

SqlCommand cmd = new SqlCommand("SELECT * FROM Customer WHERE (CustomerID=@id AND CustomerName=@name)", connection);
cmd.Parameters.AddWithValue("@id", TextBox1.Text);
cmd.Parameters.AddWithValue("@name", TextBox2.Text);

If database record found/Not found

SqlDataReader dtr = cmd.ExecuteReader();

 //If Matched with database
 if (dtr.Read())
 {
    //Your Actions
 }

 //If Not Matched With database
 else
 {
    //Your Actions
 }

Creating Cookies

HttpCookie UsernameCookies = new HttpCookie("<Your Cookie names>");
UsernameCookies.Value = TextBox1.Text;

//Add the expiry date and time of the cookies. Example
//DateTime.Now.AddHours(1), after 1 hours cookies will be removed.
UsernameCookies.Expires = DateTime.Now.AddHours(1);

//Lastly, add the cookies.
Response.Cookies.Add(UsernameCookies);
0

I'd recommend that you take each concept seperately with regard to creating threads. Each of those items may result in tons of content and alot of follow up questions. If all of those questions are discussed in one thread, it will be difficult to follow.

I'll add some info with regard to question 1:

1) if i do a form with 2 textboxes and i want to check if the value of the 2 textboxes are matching with the 2 records of my database (sql server), ho can i pass this 2 value??

You can use a SQL connection to connect to a MSSQL database. after you read the results, you can assign those results to variables you create in that procedure. You can then compare those values to the values stored in the textboxes.

For example, in c#...a simple If..Else block to determine if there is a match...

// your SQL code
// you assigned your result to a variable called strResult somewhere

string strName = TextBox1.Text;

if (strResult == strName)
{
    // result is true...do something...
}
else
{
    // result is false.. do something else...
}

As you mentioned...asp.net is not for the faint at heart. Its also not Classic ASP vNext. However, it is very powerful framework (IMHO) and great for intranet business apps in an organization that is primarily runs Microsoft software.

Again, I would proceed with very specific questions.... it doesn't matter how many threads you create. Its more important to be focused on the topic at hand.

Edited by JorgeM

0

glad this solved your initial questions, keep them coming..

Edited by JorgeM

0

Hi,
this string gave me an error :
HttpCookie UsernameCookies = new HttpCookie("nomeutente");
why?

How can i make the connection to the database for this?
SqlCommand cmd = new SqlCommand("SELECT * FROM Customer WHERE (CustomerID=@id AND CustomerName=@name)", connection);

How can i show this cookies in a label?
Response.Cookies.Add(UsernameCookies);

Thanks for all the support

0

this string gave me an error : HttpCookie UsernameCookies = new HttpCookie("nomeutente"); why?

What error did you get?

You are trying to create a cookie... here is a sample that you can refer to..

HttpCookie myCookie = new HttpCookie("lastUserVisit");
myCookie.Value = DateTime.Now.ToString();
myCookie.Expires = DateTime.Now.AddDays(365);
Response.Cookies.Add(myCookie);

How can i make the connection to the database for this?
SqlCommand cmd = new SqlCommand("SELECT * FROM Customer WHERE (CustomerID=@id AND CustomerName=@name)", connection);

You are referring to a variable in that cmd called "connection". Where is the code for creating the connection and assigning it to that variable.

For example..

connection = new SqlConnection();  
connection.ConnectionString = ConfigurationManager.ConnectionStrings["DBName"].ConnectionString;  

How can i show this cookies in a label? Response.Cookies.Add(UsernameCookies);

What do you mean show? The value of the of the cookie?

if(Request.Cookies["UsernameCookies"] != null)
{
    HttpCookie myCookie = Request.Cookies["UsernameCookies"];
    Label1.Text = Server.HtmlEncode(myCookie.Value);
}
0

This perfect!!! The error was given because i did mistake to write the proper name of the cookies... sorry ^_^

With all this code i could make working my first page ^_=
Thanks again!!!!

For the connection to sql, i was wondering if i could put it inside the webconfig once and use for all my pages!!

Thanks again!!!!

0

For the connection to sql, i was wondering if i could put it inside the webconfig once and use for all my pages!!

yes, of course placing the connection string in the web.config is the appropriate place especially if you want to reference this string in multiple pages.

Web.config

  <connectionStrings>
    <add name="dbName" connectionString="Data Source=hostname;Initial Catalog=dbName;Integrated Security=True;" providerName="System.Data.SqlClient"/>
  </connectionStrings>

In your aspx page...

using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["dbName"].ConnectionString))
        {
            SqlCommand command = new SqlCommand();
            command.Connection = connection;
            command.CommandText = "select * from table1";

            connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                // Do something...
            }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.