473,320 Members | 1,922 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem in select query in c#

con.open();
string str="select * from table1 where no="+int32.parse(txtno.text)+"";
sqlcommand com=new sqlcommand(str,con);
com.executenonquery();
con.close();

i am not getting any error msg but the data is not fetching from database pls help me wat mistake i did.
Mar 8 '11 #1
1 873
Aimee Bailey
197 Expert 100+
Use parameters and a data adapter instead, this way the library can format your query correctly data types and all, plus using the SqlDataAdapter you can dump the data into a return value like so:

Expand|Select|Wrap|Line Numbers
  1. public DataTable ReturnData()
  2. {
  3.     string ConnectionString = "connection string goes here";
  4.     DataTable data = new DataTable();
  5.  
  6.     using (SqlConnection conn = new SqlConnection(ConnectionString))
  7.     {
  8.         conn.Open();
  9.         SqlCommand cmd = new SqlCommand("select * from table1 where no = @p");
  10.         cmd.Parameters.AddWithValue("p", int32.parse(txtno.text ));
  11.         using (SqlDataAdapter ad = new SqlDataAdapter(cmd))
  12.         {
  13.             ad.Fill(data);
  14.         }
  15.         conn.Close();
  16.     }
  17.  
  18.     return data;
  19. }
  20.  
Note that in your query you use @p where as when you use the AddWithValue(string, object) function you drop the @ and only provide p.

Using the Fill method also has the benefit that this function return's a DataTable that can instantly be used in your code, each of the columns in the table are named, so this would work for example:

Expand|Select|Wrap|Line Numbers
  1.  
  2. DataTable result = ReturnData();
  3.  
  4. MessageBox.Show(result.Rows[0]["column_name"].ToString());
  5.  
  6.  
Hope this helps!

Aimee.
Mar 8 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Vic Spainhower | last post by:
Hello, I am new to php and MySQL and I'm attempting to run a select query on a MySQL database which is working fine except prior to displaying the table with the results from the select query it...
1
by: Roberto Esposito | last post by:
HI to All.... i need an help for a Select Query MYSQL Version 3.23.58 it's all day i try to resolve this problem.... but i don't find SOLUTION! sigh :-( This is the simple query.....
0
by: Jean | last post by:
Hi all, This problem has me dumbstruck at the moment and I was wondering if someone could offer some advice while I have a go at it myself. Background: I have a query qryAll that takes data...
1
by: Greg Strong | last post by:
Hello All, Any reason why a select query that runs will not run as an update query? What I've done is created a select query that runs successfully. The query has several custom functions to...
6
by: jjturon | last post by:
Can anyone help me?? I am trying to pass a Select Query variable to a table using Dlookup and return the value to same select query but to another field. Ex. SalesManID ...
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
0
by: djflow | last post by:
Hi! II was wondering if you can help me with SQL query.. Below 7 separated select query works fine(only when they are retrieved separately) But I want to combined them together and so that i...
2
by: grabit | last post by:
Hi Mary this is the problem child (query) <!---Query db for page info---> <cfquery name="showtopics" datasource="parrots"> SELECT categories.catID, threads.threadID, threads.posttype,...
3
by: RAG2007 | last post by:
I'm using the QueryDef and Execute method to update a record in my MySQL backend. Problem: When the Passthrough update query is defined using QueryDef, it becomes a select query, and I cannot use...
1
by: atishrg | last post by:
Hello All, I am facing one strange problem.. I am using a select query in my function which will return specified product details according to product code selected by user.. here is the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.