locked
I cant get sql read to work. RRS feed

  • Question

  • User-424182210 posted

    In my code I can open a sql channel ok.

    However I cant get it to read a table with a username and password field.

    I get username not a valid object error.

               
                string connectionString = @"Server = (localdb)\MSSQLLocalDB; connect timeout=30; database=Database1; Integrated Security = false; ";
    
    
                SqlConnection connection = new SqlConnection();
                
                    connection.ConnectionString = connectionString;
    
                    try
                    {
                        connection.Open();
                    }
                    catch (Exception err)
                    {
                        Console.WriteLine(err.ToString());
                    }
                
      
    
    
    
    
    
                try
                {
                    SqlDataReader myReader = null;
                    SqlCommand myCommand2 = new SqlCommand(
                    "SELECT * from [username];", connection);
    
    
    
                    myReader = myCommand2.ExecuteReader();
                    while (myReader.Read())
                    {
                        TextBox1.Text = myReader["username"].ToString();
                    }
                }
                catch (Exception er)
                {
                    Console.WriteLine(er.ToString());
                }
    
    
    

    Monday, February 8, 2016 1:26 AM

Answers

  • User2103319870 posted

     while (myReader.Read())

    {

    TextBox1.Text = myReader["username"].ToString();

    }

    Ensure that the columnname which you get from username table is equal to what you used in above code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 2:00 AM
  • User-2057865890 posted

    Hi 75577557,

    SqlDataReader objects allow you to read data in a fast forward-only manner. You obtain data by reading each row from the data stream. 

    HasRows enables you to determine if the DataReader has returned any results before reading from it.

    As a2h said, ensure that the columnname which you get from username table is equal to what you used in above code

    Take a look at the following code snippets.

    class Program
        {
            static void Main(string[] args)
            {
                SimpleRead();
            }
            public static void SimpleRead()
            {
                // declare the SqlDataReader, which is used in
                // both the try block and the finally block
                SqlDataReader rdr = null;
    
                // create a connection object
                SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
    
                // create a command object
                SqlCommand cmd = new SqlCommand("select * from Customers", conn);
    
                try
                {
                    // open the connection
                    conn.Open();
    
                    // 1. get an instance of the SqlDataReader
                    rdr = cmd.ExecuteReader();
    
                    // print a set of column headers
                    Console.WriteLine("Contact Name             City                Company Name");
                    Console.WriteLine("------------             ------------        ------------");
    
                    // 2. print necessary columns of each record
    
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            // get the results of each column
                            string contact = (string)rdr["ContactName"];
                            string company = (string)rdr["CompanyName"];
                            string city = (string)rdr["City"];
    
                            // print out the results
                            Console.Write("{0,-25}", contact);
                            Console.Write("{0,-20}", city);
                            Console.Write("{0,-25}", company);
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                    }
                }
                finally
                {
                    // 3. close the reader
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
    
                    // close the connection
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
        }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 11:52 AM

All replies

  • User2103319870 posted

     while (myReader.Read())

    {

    TextBox1.Text = myReader["username"].ToString();

    }

    Ensure that the columnname which you get from username table is equal to what you used in above code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 2:00 AM
  • User-2057865890 posted

    Hi 75577557,

    SqlDataReader objects allow you to read data in a fast forward-only manner. You obtain data by reading each row from the data stream. 

    HasRows enables you to determine if the DataReader has returned any results before reading from it.

    As a2h said, ensure that the columnname which you get from username table is equal to what you used in above code

    Take a look at the following code snippets.

    class Program
        {
            static void Main(string[] args)
            {
                SimpleRead();
            }
            public static void SimpleRead()
            {
                // declare the SqlDataReader, which is used in
                // both the try block and the finally block
                SqlDataReader rdr = null;
    
                // create a connection object
                SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");
    
                // create a command object
                SqlCommand cmd = new SqlCommand("select * from Customers", conn);
    
                try
                {
                    // open the connection
                    conn.Open();
    
                    // 1. get an instance of the SqlDataReader
                    rdr = cmd.ExecuteReader();
    
                    // print a set of column headers
                    Console.WriteLine("Contact Name             City                Company Name");
                    Console.WriteLine("------------             ------------        ------------");
    
                    // 2. print necessary columns of each record
    
                    if (rdr.HasRows)
                    {
                        while (rdr.Read())
                        {
                            // get the results of each column
                            string contact = (string)rdr["ContactName"];
                            string company = (string)rdr["CompanyName"];
                            string city = (string)rdr["City"];
    
                            // print out the results
                            Console.Write("{0,-25}", contact);
                            Console.Write("{0,-20}", city);
                            Console.Write("{0,-25}", company);
                            Console.WriteLine();
                        }
                    }
                    else
                    {
                        Console.WriteLine("No rows found.");
                    }
                }
                finally
                {
                    // 3. close the reader
                    if (rdr != null)
                    {
                        rdr.Close();
                    }
    
                    // close the connection
                    if (conn != null)
                    {
                        conn.Close();
                    }
                }
            }
        }

    Best Regards,

    Chris Zhao

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 8, 2016 11:52 AM