Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • Last Modified:

How do I search in a database and get the values out?

I have the code below. But I dont know how to get the info out.

Lets say I find the thing I was searching for.....how can I get it out? And will the return value be null if it doesnt exist?
Boolean flag = false;
        if (Page.IsValid)
        {
            String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2005\\WebSites\\WebSite\\WebSite\\Tenta1\\Nwind.mdb";
            OleDbConnection myConnection = new OleDbConnection(myConnectionString);
            string myInsertQuery = "SELECT * FROM Customers WHERE (CustomerID) ='" + SearchTextBox.Text + "'";          
            OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
            myCommand.Connection = myConnection;
            myConnection.Open();
            myCommand.ExecuteNonQuery();
            myCommand.Connection.Close();

Open in new window

0
Mickeys
Asked:
Mickeys
  • 2
  • 2
1 Solution
 
cauosCommented:
first of all let me clerify one thing for you:
ExecuteNonQuery(); used to executes a Transact-SQL statement against the connection and returns the number of rows affected by executing UPDATE, INSERT, or DELETE statements.
ExecuteReader():Executes the CommandText against the Connection, and returns an DbDataReader.by executing SELECT statement .
ExecuteScalar():Executes the query and returns the first column of the first row in the result set returned by the query. All other columns and rows are ignored.

and in your case you should use ExecuteReader() because you return one record from the reader.

in the example below i assume you have text boxs and you want to bind the data return to those textBoxs
Boolean flag = false;
        if (Page.IsValid)
        {
            String myConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source= C:\\Documents and Settings\\MFL\\Dokumenter\\Visual Studio 2005\\WebSites\\WebSite\\WebSite\\Tenta1\\Nwind.mdb";
            OleDbConnection myConnection = new OleDbConnection(myConnectionString);
            string myInsertQuery = "SELECT * FROM Customers WHERE (CustomerID) ='" + SearchTextBox.Text + "'";          
            OleDbCommand myCommand = new OleDbCommand(myInsertQuery);
            myCommand.Connection = myConnection;
            myConnection.Open();
            OleDbDataReader rdr=myCommand.ExecuteReader();
rdr.Read();
txtBox1.Text=rdr[0].ToString();
......
....
txtBox2.Text=rdr[1].ToString();
txtBoxn.Text=rdr[n].ToString();
            myConnection.Close();

Open in new window

0
 
MickeysAuthor Commented:
aha. but when I try to run the code I get
+            rdr[0]      'rdr[0]' threw an exception of type 'System.InvalidOperationException'      object {System.InvalidOperationException}

Is the statement wrong?
0
 
MickeysAuthor Commented:
Hmm it seems if I write somehting that doesnt exicst i get that?? Could that be correct?
0
 
cauosCommented:
if there is no record return this error will occur; and to be in the safe side use the line of code
 if (rdr.HasRows)
        {
        txtBox1.Text=rdr[0].ToString();
......
....
txtBox2.Text=rdr[1].ToString();
txtBoxn.Text=rdr[n].ToString();

        }
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now