Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 27-May-12 23:19pm
Edited 27-May-12 23:40pm
v2
Comments
OriginalGriff at 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 at 28-May-12 4:40am
   
Sorry about that, i just improved the question
sunandandutt at 28-May-12 4:25am
   
I agree with OriginalGriff
Ahmed Ibrahim Assaf at 28-May-12 4:31am
   
I agree with OriginalGriff, This no helpful at ALL !!
Rate this: bad
good
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))
                {
                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:
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?
  Permalink  
Comments
Member 8956437 at 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 at 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 at 28-May-12 5:10am
   
Nevermind i had an error in the connection string, thanks for everything
Rate this: bad
good
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()
       da.Fill(ds)
       CheckBox1.Text = ds.Tables(0).Rows(0)(0).ToString()
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 334
1 Kornfeld Eliyahu Peter 325
2 Maciej Los 239
3 OriginalGriff 188
4 King Fisher 185
0 OriginalGriff 6,303
1 DamithSL 4,764
2 Maciej Los 4,306
3 Kornfeld Eliyahu Peter 3,914
4 Sergey Alexandrovich Kryukov 3,538


Advertise | Privacy | Mobile
Web04 | 2.8.141220.1 | Last Updated 28 May 2012
Copyright © CodeProject, 1999-2014
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