Click here to Skip to main content
13,097,884 members (56,897 online)
Rate this:
Please Sign up or sign in to vote.

Is there a way, in aspx.cs, to read data from a database? I doesnt seem to be exactly the same as in C#.
I need to use the select command to read from the database and put the value i just read in a variable, just a simple SQL Select command, i think i might be missing some syntax.

Thank You.
Posted 27-May-12 22:19pm
Updated 27-May-12 22:40pm
OriginalGriff 28-May-12 4:23am
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Use the "Improve question" widget to edit your question and provide better information.
Member 8956437 28-May-12 4:40am
Sorry about that, i just improved the question
sunandandutt 28-May-12 4:25am
I agree with OriginalGriff
Ahmed Ibrahim Assaf 28-May-12 4:31am
I agree with OriginalGriff, This no helpful at ALL !!
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

If you are using C#, then the code is going to be pretty much the same: just use a parametrized query.
using (SqlConnection con = new SqlConnection(strConnect))
    using (SqlCommand com = new SqlCommand("SELECT iD, description FROM myTable WHERE Id=@ID", con))
        com.Parameters.AddWithValue("@ID", id);
        using (SqlDataReader reader = com.ExecuteReader())
            while (reader.Read())
                int id = (int) reader["iD"];
                string desc = (string) reader["description"];
                Console.WriteLine("ID: {0}\n    {1}", iD, desc);

using (SqlConnection con = new SqlConnection(strConnect))
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con))
        da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text);
        DataTable dt = new DataTable();
        myDataGridView.DataSource = dt;

Or is there something more specific your are trying to do that is failing?
Member 8956437 28-May-12 4:57am
Thats what i've been trying, but i keep getting an error :
Server Error in '/WebSite' Application.
Invalid object name 'servidores'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'servidores'.

Servidores is my Table name.
OriginalGriff 28-May-12 5:05am
Difficult to be at all precise: basically SQL is saying that it can't find your table, and there are a huge number of possible reasons why not which include:
You could have spelt the table name wrong
You could have connected to the wrong SQL server instance
You might have not attached your DB to the server
You might not have created the tables in that server instance.
Your user account on SQL server may not have SELECT privileges.

For testing, cut your code down to a bare minimum: similar to the first example in by answer above, and try it. Check your connection string, check your database. If you can access the database on that instance from your development Pc via SSMS, do it, and check that way.
Get the limited code working first, then scale up to your real app.
Member 8956437 28-May-12 5:10am
Nevermind i had an error in the connection string, thanks for everything
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

see this it may help you

da = New SqlDataAdapter("select distinct( name) from sample", con)
       ds = New DataSet()
       CheckBox1.Text = ds.Tables(0).Rows(0)(0).ToString()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.170813.1 | Last Updated 28 May 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100