Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,


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.

Regards.
Thank You.
Posted
Updated 27-May-12 22:40pm
v2
Comments
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 !!

If you are using C#, then the code is going to be pretty much the same: just use a parametrized query.
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    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);
                }
            }
        }
    }

Or:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    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();
        da.Fill(dt);
        myDataGridView.DataSource = dt;
        }
    }


Or is there something more specific your are trying to do that is failing?
 
Share this answer
 
Comments
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
see this it may help you

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

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900