Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Sir


i have product id in table like 1,2,3 and i want to select data where productid=1 .
i used in query i does not work any solution
Posted
Comments
thatraja 10-Feb-14 4:27am    
'does not work'? what's the error?
[no name] 10-Feb-14 4:27am    
please share what you tried..
rajbir singh thind 10-Feb-14 4:31am    
i tried select * from table where productid in('1',;2') it results no
seconde like query but it show result if product id is 10 and 1
thatraja 10-Feb-14 4:33am    
That's wrong query, try this
select * from table where productid in('1','2')
rajbir singh thind 10-Feb-14 4:36am    
i tried this one but it does not result anything

Assuming your ProductId is a numeric field in SQL then:
SQL
SELECT * FROM MyTable WHERE ProductId=1
Will fetch it.

If for some reason it isn't and you have a product id that is string based (VARCHAR from example) then it gets more complex if you are trying to find the data based on a partial string:
SQL
SELECT * FROM MyTable WHERE ProductId LIKE '%1%'
May do it, but you will probably have to be rather more explicit about your table design and content, as well as the inputs and results you expect if you want more specific help.
 
Share this answer
 
Comments
Simon_Whale 10-Feb-14 10:38am    
To add to griff's answer if you end up doing a 'like' statement searching for values be aware that this will cause a full table scan, regardless if you have any indexes
Please give your table details and what you need from this table.

Or you can try this
SQL
select * from Product p where p.Id=1


I think you can got your Answer
 
Share this answer
 
Comments
rajbir singh thind 10-Feb-14 4:51am    
i have product table and detail table in detail table i saved prodtcid in var char like 1,2 and when i search product id 1 using in query it shows no result
[no name] 10-Feb-14 4:57am    
use this
select * from Product p where p.Id='1'
[no name] 10-Feb-14 5:52am    
OK its fine that you have already done your primary work.

Now we need to see your gridview structure and your business to give you solutions.
Which data you want to get from gridview, Its selected data or total row. Its from any controll or just from row.
I think you understand my point.
Assuming That you are using SQL Server

C#
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
  cn.Open();

  SqlCommmand cmd = new SqlCOmmand();
  cmd.Connection = cn;
  cmd.commandText = "select * from Table where ProductID in {@value1, @value2, @value3}";
  cmd.parameters.Add(new SqlParameter("@value1", SqlDbType.Int).Value = 1;
  cmd.parameters.Add(new SqlParameter("@value2", SqlDbType.Int).Value = 2;
  cmd.parameters.Add(new SqlParameter("@value3", SqlDbType.Int).Value = 3;

  SqlDataReader rdr = cmd.ExecuteReader();
}


I would uses these as a reference

SQL Server Data Type Mappings[^]
SqlCommand Class[^]
 
Share this answer
 
if you are using sql server and you have connected to the server you can set a sql command like


select * from Whatever where ID = '1'


if you want to select from multiple tables it would look something like this


select * from whatever s, whoever d where s.id = d.id and s.id = '1'

if you are more used to innerjoint then id suggest you use that,


a way to connect to sql ... this is not usually the way i would connect but it helps to see if your sql is correct with the table also created correct

using System.Data.SqlClient;
using System.Data.Sql;


//Creating connection to local database (connection should be converted to vpn database connection)
SqlConnection cn = new SqlConnection(@"Data Source = " + connection + "; Initial Catalog = whatever; Integrated Security = True");

//Opening connection to local database (connection should be converted to vpn database connection)
cn.Open();

// creating statement o be called in sql
SqlCommand cmd = new SqlCommand("select * from whatever s, whoever d where s.id = d . id and s.id = '1'", cn);


SqlDataAdapter sqlDataAdap = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

sqlDataAdap.Fill(ds, "ss");
datagridview.DataSource = ds.Tables["ss"];

datagridview.Columns[2].Visible = false;
datagridview.Columns[3].Visible = false;

cn.Close();

like i said ... not the best way to connect depending on what you want to do ... also your select should be close to the way you created your table .... e.g.

create table whatever
(
1 varchar(100) not null, // this wil be a string input e.g 'anything' [select * from whatever where 1 = 'anything']
)

insert into whatever values ('anything')

this is a int

create table whatever
(
test int not null
)

insert into whatever values (1)

select * from whatever where test = 1
 
Share this answer
 
SELECT * FROM Table WHERE ProductId LIKE '1%'
 
Share this answer
 
SELECT * FROM tablename WHERE ProductID = 1
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900