How to get fields from database

I want to get "Model" field and put on Textbox6. But how come it does not work. The problem is that the Model field answer will not be shown in the textbox6

string Query = "Select * from S where Name = '" + TextBox1.Text + "' and Clientno = '" + TextBox2.Text + "';"; command.CommandText = Query; OleDbDataReader reader = command.ExecuteReader(); while (reader.Read()) { string Model = reader.GetString(reader.GetOrdinal("Model")); TextBox6.Text = Model; }

-------------Problems Reply------------

Couple of things:

  • Do not use select *, instead use select your columns names
  • Do not pass the .Text directly to your query, instead use parameterized sql expression
  • If Clientno is primary key column or , combination of Name and ClientNo gives unique result, use ExecuteScalar, you don't have to use ExecuteReader and loop through the datareader

Since you using only one field and want to fill in the textbox, modify your select statement to :

select top 1 Model from S where....

And if you are reading only one row you will not need a while loop. Further, you should always close the reader and put your SqlConnection inside using block. ( edited as suggested by the comments)

If (reader.Read())
TextBox6.Text = reader.GetString(reader.GetOrdinal("Model"));

Category:c# Views:0 Time:2018-02-08
Tags: sql

Related post

  • how to get value from database into Expandable List via cursor 2011-03-07

    I wonder how to get value from database into Expandable List via cursor public class SmplExpandable extends ExpandableListActivity { private static final String NAME = "NAME"; private static final String IS_EVEN = "IS_EVEN"; private ExpandableListAda

  • How to get data from database using reverse ajax 2011-06-15

    I am new to DWR; I am trying to get data from database using reverse Ajax. But i am confuse how to do this. Can any one tell me the process with simple example...? I want to use comet... I am using Spring Framework, Jsp, Mysql Thanks

  • How to get object from database using sqlMap? 2012-01-29

    I want to insert and get object from database using sqlMap. insert user is working fine but when i call getUser it throw an exception "Request processing failed; nested exception is java.lang.NullPointerException". SqlUserDaoImpl public void insertUs

  • How to get value from database with onListItemClick? 2011-04-09

    I can add to a db and list as a listview. When I click a list item using onListItemClick, what statement do I need to get the value? It's urgent; please explain with code. Thanks in advance. String[] from = new String[] {NotesDbAdapter.KEY_BODY, Note

  • how to get date from database and display without any repetition? 2011-07-05

    I had a function which is pulling all the dates from database against each record. public function getAllYears() { $collection = Mage::getModel('press/press')->getCollection()->getYears(); return $collection; } and am displaying it as : <?ph

  • How to get values from database[Cursor Adapter] and store it to an array in java? 2011-12-19

    I would like to get the values from database of a particular column by executing a query. Is it possible to do it after we do it to the Cursor Adapter or can we attain the values well before itself. Kindly help on this with a snippet or a guide. ----

  • Sharepoint 2007 - how to get fields from automatically created lists into dropdown lists 2010-12-07

    I have created a sharepoint list using C sharp and the MS sharepoint library, by building site columns into content types and finally into a list. I am now using Sharepoint Designer to create a page with a drop down list that will display fields from

  • How to get fields from such a string? 2009-11-20

    I'm importing some data from a database. The data has been stored by a CMS written in php where I have no control. Here is the data (a dense report from a paypal response): a:56:{ s:8:"business";s:19:"[email protected]"; s:14:"receiver_email";s:19:

  • How to get data from database but from the exact row? 2010-05-03

    I am using mysql. Now I want to write that query in which i will give mysql the row number then mysql will data from that column no only. means.. if i have twenty rows in database but i want to get only 10th no of rows data? so how to write that kind

  • How to get fields from secondary list that are not identifed in the lookup into a single view? 2009-07-31

    I have 2 lists in SharePoint and one of them is a list of applications and another is tasks. The task list has a lookup column pointed at the App List: Name column. My App list contains some other information such as Department Name. TASK LIST - Work

  • How to get result from database on the change of values in text box using Ajax? 2010-09-27

    I am working in PHP and Mysql Combo I need to search the results from DB and store in a hidden field , on change of the value user inserts in a text box. How can I do this Using Ajax? --------------Solutions------------- You will need a few things: Y

  • How to get sqlite from databases folder in device? 2012-04-23

    Hello i had implemented this code Using your own SQLite database in Android applications It takes my db from assets folder and copy it in databases folder into device. Now how could i get that databse from my device ? or to get is just possible if my

  • How to get Result from database by order the condition put in "IN" clause? 2010-07-12

    Sorry for bad english. bfproduct is a table and productid is primary key in this table and productname is another field defined in this table. When i execute this query, select * from bfproduct where productid in (23,5,54,3132,32). The result is as f

  • how to get records from database based on date range in linq 2011-06-21

    I have develop a small web application in that i'm using entity frame work .i want get the records based on the date range and bind that data in gridview how can i write a query using linq..please help me..Here i have post my code what i have try to

  • How to get time from database in Rails? 2011-07-08

    How do you execute a SQL statement from Rails 3? For example, the SQL statement "SELECT CURRENT_TIME" gets the time (working in PostgreSQL). Model.find_by_sql("SELECT CURRENT_DATE") returns a model object. --------------Solutions------------- If you

  • How to SELECT fields from Database if you don't have the variable you need? 2011-08-18

    I asked a similar question before but deleted it because i couldn't explain it correctly. This states it perfectly. I have 4 tables: Clients Agents Client_agent_aprovals Subscriptions - Clients table has: id, client_id, client_name Agents table has:

  • How to check field from database with jquery and show check or cross? 2011-12-02

    I have a page and I ask them zipcode. While they are filling the form right after they finish writing 5 numbers of zipcode, It will check if it is covered from my database and will show a check or cross sign near it and will disable submit. To summar

  • How to get values from database loop and add them up for later use 2012-02-20

    I am querying the database for different values of prizes for an online loyalty scheme. I know this is going to be a sort of easy question but Im still getting used to PHP so here goes... This is what I have so far: <?php $sqlprize="SELECT prizeid

  • How to get Images from Database using Breeze? 2013-12-14

    We have Images stored in SQL Server database, I use HotTowel's template(thanks to John Papa) My Model is like this: public class Product { public int Id { get; set; } public string ProductName{ get; set; } [DataType("image")] public Byte[] ImageData

  • How to get data from database given range of date and time 2015-03-19

    There is a database which has data stored on it based on date and time (yyyy-mm-dd hh:mi:ss). However I need data based on range of date and time. For eg. I need data from 03/11/15 to 03/18/15 only during time 15:00 to 18:00. How can I do this? Do I

  • How to get Items from Database to Combobox and textBox 2010-06-26

    I have a table from Sqlite: KHACHHANG(id,name,email,phone,address). I want to create a windows form which show a combobox of , when I choose a name in that list . Another textboxes ( Email,Phone,Address ) will auto fill in :) Thanks so

  • How to get data from database in Android ? by Cursor c= db.query? 2011-05-30

    THis is myDB Class //Please take a look at public Cursor getplaces() --- Am i right or wrong? and How to code in main Activity class if i want to setListAdapter. Thank you for your help. public class MyDB { protected static final String TAG = "TAG";

  • How to get objects from database django? 2013-09-08

    I have a little problem. I'm trying to use django filter e.g. SomeTable.objects.filter(someParam="something") this is working well but... What if i have param name like string in some variable like: test="someParam". How to now call filter function?

  • Get field from JsonPath in java 2014-12-15

    How to get field from a:b:c:d:f:1.0 property via JsonPath? "a:b:c:d:f:1.0" : { "field" : "field"} I tried with a:b:c:d:f:1.0.field but returns invalid path. --------------Solutions------------- Please check your syntax in the above. You have missed t

Copyright (C), All Rights Reserved.

processed in 0.264 (s). 11 q(s)