Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,105 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,695 people online right now. Registration is fast and FREE... Join Now!




How to display SQL search result in a textbox

 
Reply to this topicStart new topic

How to display SQL search result in a textbox

toe_toe7
23 Sep, 2007 - 06:46 PM
Post #1

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 5


My Contributions
I have the following code. And I do not know how to display the result from the SELECT SQL Statement into a textbox.

Thank you in advance.



CODE

string dbConn = ConfigurationSettings.AppSettings["DatabaseConn"];
OleDbConnection loginConn = new OleDbConnection(dbConn);
loginConn.Open();

OleDbCommand getOwnerQuery = new OleDbCommand("SELECT EMPNAME FROM EMPLOYEE WHERE EMPNO = '" + ownerEmpNo + "'", loginConn);
getOwnerQuery.ExecuteReader();




User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Display SQL Search Result In A Textbox
23 Sep, 2007 - 07:06 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
For starters, if this is a web application, you're a prime target for a SQL Injection Attack by using that inline SQL, very bad choice.

Second, to do this simply create a variable, assign it to the return results, then assign that to the textbox:

CODE

string dbConn = ConfigurationSettings.AppSettings["DatabaseConn"];
OleDbConnection loginConn = new OleDbConnection(dbConn);
loginConn.Open();

OleDbCommand getOwnerQuery = new OleDbCommand("SELECT EMPNAME FROM EMPLOYEE WHERE EMPNO = '" + ownerEmpNo + "'", loginConn);
OleDbDataReader reader = getOwnerQuery.ExecuteReader();
TextBox1.Text = reader[0].ToString();

User is offlineProfile CardPM
+Quote Post

toe_toe7
RE: How To Display SQL Search Result In A Textbox
23 Sep, 2007 - 07:56 PM
Post #3

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 5


My Contributions
Hi PsychoCoder,

First, thank you very much for your pointer regarding the SQL Injection Attack.


Second, I have the following error when i used reader[0].

" No data exists for the row/column. "

Any idea? Thank you in advance.

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Display SQL Search Result In A Textbox
23 Sep, 2007 - 08:02 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well it could be because there isnt any data to read, to protect against that put it in a while block

CODE

string dbConn = ConfigurationSettings.AppSettings["DatabaseConn"];
OleDbConnection loginConn = new OleDbConnection(dbConn);
loginConn.Open();

OleDbCommand getOwnerQuery = new OleDbCommand("SELECT EMPNAME FROM EMPLOYEE WHERE EMPNO = '" + ownerEmpNo + "'", loginConn);
OleDbDataReader reader = getOwnerQuery.ExecuteReader();
while(reader.read())
{
     TextBox1.Text = reader["FieldName"].ToString();
}
reader.Close();


Instead of using an index for the reader, use the column name and see if that fixes it

Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

toe_toe7
RE: How To Display SQL Search Result In A Textbox
23 Sep, 2007 - 09:49 PM
Post #5

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 5


My Contributions
Thanks a lot, Psycho..smile.gif..
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 09:12PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month