Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2009-05-03
4
163 Views
Last Modified: 2012-05-06
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
Comment
Question by:Mickeys
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
cauos earned 500 total points
ID: 24289582
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
 

Author Comment

by:Mickeys
ID: 24289623
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
 

Author Comment

by:Mickeys
ID: 24289634
Hmm it seems if I write somehting that doesnt exicst i get that?? Could that be correct?
0
 
LVL 4

Expert Comment

by:cauos
ID: 24289723
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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question