1

I am new to C# and I have connected it with Oracle11g and using Visual Studio 2013 as a tool. I am trying to display a 'name ' that is being returned by a query to a textbox but it is neither displaying an Error Message nor displaying the Output. Pleases help me to resolve this problem. Thanks... here is my code..

private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string oradb = "Data Source=ORCL;User Id=hr; Password=123;";
            OracleConnection conn = new OracleConnection(oradb);  // C#
            conn.Open();
            OracleCommand cmd = new OracleCommand();
            cmd.Connection = conn;
            cmd.CommandText = "select name from std where cgpa=2.82;";
            cmd.CommandType = CommandType.Text;
            OracleDataReader dr = cmd.ExecuteReader();
            dr.Read();
            textBox1.Text = dr.GetString(0);
             conn.Dispose();
        }
        catch (Exception ex) { MessageBox.Show("\n"+ex); }
    }

after setting textBox1.Text = dr.GetString(0); it is giving me the attached exception.enter image description here

JAMSHAID
  • 1,258
  • 9
  • 32

1 Answers1

2

Array indexes start at index zero, not 1. The returned string (if any) is at

textBox1.Text = dr.GetString(0);

A more correct way to write your code is the following

private void button1_Click(object sender, EventArgs e)
{
    try
    {
        string oradb = "Data Source=ORCL;User Id=hr; Password=123;";

        // Use the using statements around disposable objects....
        using(OracleConnection conn = new OracleConnection(oradb))
        using(OracleCommand cmd = new OracleCommand())
        {
             conn.Open();

             // These two parameters could be passed directly in the
             // OracleCommand constructor....
             cmd.Connection = conn;
             cmd.CommandText = "select name from std where cgpa=2.82;";

             // Again using statement around disposable objects
             using(OracleDataReader dr = cmd.ExecuteReader())
             {
                  // Check if you have a record or not
                  if(dr.Read())
                      textBox1.Text = dr.GetString(0);
             }
        }
    }
    catch (Exception ex) { MessageBox.Show("\n"+ex); }
}

And if your code is supposed to return just a single record with a single column then you can use the more performant ExecuteScalar without building an OracleDataReader

 // Again using statement around disposable objects
 object result = cmd.ExecuteScalar();
 if(result != null)
     textBox1.Text = result.ToString();
Steve
  • 213,761
  • 22
  • 232
  • 286
  • 1
    I have did the same as you said but it is showing me an exception. I have updated the question containing that exception. please take a look again at the question. thanks – JAMSHAID Sep 30 '17 at 07:41
  • The error is given on the Open call for the connection. This means that your connection string is wrong. – Steve Sep 30 '17 at 07:42
  • 1
    What should the connection string be if it is wrong. I am using hr account with password 123. – JAMSHAID Sep 30 '17 at 07:45
  • Something is stopping your provider in reaching the database. I could only suggest to look around for the possible causes of the error ORA-12154 – Steve Sep 30 '17 at 07:49
  • For example this one https://stackoverflow.com/questions/20361325/this-is-error-ora-12154-tnscould-not-resolve-the-connect-identifier-specified – Steve Sep 30 '17 at 07:51
  • A full reference to various ways to create a connection string for Oracle could be found at https://www.connectionstrings.com/net-framework-data-provider-for-oracle/ – Steve Sep 30 '17 at 07:55