Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
select item1,item2 from items where item1='"+textbox1.text+"'

how can i recieve result in two variables one for item1 and another for item2
Posted
Comments
Oshtri Deka 18-Apr-11 7:48am    
Being brief is nice quality, but it can be flaw too. In future, please give us more information.
Mohammed Ahmed Gouda 19-Apr-11 4:03am    
okay and am sorry my english is bad :)

string item1, item2;
SqlDataAdapter adapter = new SqlDataAdapter(mycommand);
DataTable dt=new DataTable();
adapter.Fill(dt);
item1 = dt.Rows[0]["item1"].ToString();
item2 = dt.Rows[0]["item2"].ToString();
 
Share this answer
 
Get the data using DataAdapter.Fill in DataTable and use dt.Rows[0].Item[0] and dt.Rows[0].Item[1]

or

use ExecuteReader to get data in SqlDataReader and use sr.Item[0] and sr.Item[1]

or

use ExecuteScalar twice with item1 and item2 separately.
 
Share this answer
 
select item1,item2 from items where item1='"+textbox1.text+"'

execute this query and take result into DataSet(ds)
now
var result1 = ds.Tables[0].Rows[0]["item1"]

var result2 = ds.Tables[0].Rows[0]["item2"]

This is one way you can do the things ,

Another is

Write a stored procedure having two query like

select item1 from items where item1='"+textbox1.text+"'

select item2 from items where item1='"+textbox1.text+"'

and now execute above SP and take result into one dataset and do following things,

var Result1= ds.Tables[0].Rows[0]["item1"]

var Result2 = ds.Tables[1].Rows[0]["item2"]

I hope this will help you.

All d Best.
 
Share this answer
 
v2
Comments
Prasanta_Prince 18-Apr-11 8:43am    
Its the correct Solution.
Use this as a guideline. You should check returned values for DBNull as well.

var item1, item2;


//you must put code for creating and defining command by yourself

try
{
   SqlDataReader reader = myCommand.ExecuteReader();

   while(reader.Reads())
   {  
      //for the example purposes I'll assume int is correct data type,
      //otherwise use  proper Get method
      item1 = reader.GetInt32(0);
      item2 = reader.GetInt32(1);

      //do something with acquired data...
   }
}
catch
{
   //exception handling or just throw it
}
finally
{
   reader.Close();
   myCommand.Dispose();
}
 
Share this answer
 
v4

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