how to read data from the database?

Posted by Rimjhim4 under ASP.NET on 3/15/2012 | Points: 10 | Views : 3843 | Status : [Member] | Replies : 11
i have a table signup in database, i want to display table through dataset in c#.how it is possible




Responses

Posted by: Sabarimahesh on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
HI Friends

Example:

SqlDataReader Reader= null;

SqlConnection Con= new SqlConnection("server=(local)\SQLExpress;Integrated Security=SSPI;database=xxx");
SqlCommand cmd= new SqlCommand("SELECT id, lname, fname, age, address FROM Employees", Con);

con.Open();
Reader= cmd.ExecuteReader(CommandBehavior.CloseConnection);

Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
hi

you should fill data table and then you can use row property

see this code

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString);

SqlCommand cmd = new SqlCommand("store_procedure", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = con;
con.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();
adp.Fill(dt);


textbox1.text=dt.Rows[RowIndex].ItemArray[ColumnName].ToString();

cmd.Dispose();
con.Close();
con.Dispose();



Mark as Answer if it helps
Thanks






R D Patel

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
hi

you can also use dataset instead of table

code to get value from dataset
suppose ds is your dataset then after filling ds
use this code

Ds.Tables[0].Rows[0]["ColumnName"].ToString();


Thanks

R D Patel

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
After
That
SqlCommand

We Can Read by using Below Code
SqlCommand objCommand = new SqlCommand("SELECT id, lname, fname, age, address FROM Employees", dbCon.sqlcon);
objCommand.CommandType = CommandType.Text;
SqlDataReader read=new SqlDataReader()
id=read.GetValue(0).ToString();
lname=read.GetValue(1).ToString();
fname=read.GetValue(2).ToString();
age=read.GetValue(3).ToString();
address =read.GetValue(4).ToString();
read.Read();
cmd.Executenonquery()



Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rimjhim4 on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
what is row index here????
it gives error at rowindex..........

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi rimjhim

SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["cs"].ConnectionString);

SqlCommand cmd = new SqlCommand("store_procedure", con);

cmd.CommandType = CommandType.StoredProcedure;

cmd.Connection = con;

con.Open();

SqlDataAdapter adp = new SqlDataAdapter(cmd);



DataTable dt = new DataTable();

adp.Fill(dt);



int RowIndex=0;

textbox1.text=dt.Rows[RowIndex].ItemArray[ColumnName].ToString();



cmd.Dispose();

con.Close();

con.Dispose();


Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/15/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Rimjhim4,
Here is a nice example of Reading database data using SqlDataReader and store it in an array:
http://forums.asp.net/t/1656524.aspx/1

Thanks,
Sanjay

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi check this all:

http://www.java2s.com/Code/ASP/ADO.net-Database/GetdataindatabasethroughSqlDataAdapterC.htm
http://www.java2s.com/Code/ASP/ADO.net-Database/ReaddatafromdatabaseincodebehindC.htm
http://www.akadia.com/services/dotnet_data_reader.html

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Patel28rajendra on: 3/15/2012 [Member] Starter | Points: 25

Up
0
Down
Hi

for raw index you can write 0,1,2
that is raw of the table which consist data after filling it.


Thanks


R D Patel

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Denny on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,

string _strcon="@server=server name;initial catalog=db name;integrated security=true";
SqlConnection sqlcon=new SqlConnection(_strcon);
sqlcon.Open();
SqlDataAdapter dadapter=new SqlDataAdapter("Write your sql query",sqlcon);
DataSet dset=new DataSet();
dadapter.Fill(dset);
sqlcon.Close();


if using stored procedure
SqlCommand sqlcom;
SqlDataAdapter dadpater;
Dataset dset;
sqlcom = sqlcon.CreateCommand();
sqlcom.CommandText = "ProductBind";
sqlcom.CommandType = CommandType.StoredProcedure;

dadapter = new SqlDataAdapter();
dadapter.SelectCommand = sqlcom;
sqlcom.Parameters.AddWithValue("@ctgryname", cname);
dset = new DataSet();
dadapter.Fill(dset);

Regards,
Denny Kurian

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sf_Hussain786 on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
try this,


try
{
SqlCommand sqlCommand = new SqlCommand();

using (SqlConnection sqlConnection = new SqlConnection(connectionString))
{
sqlCommand = sqlConnection.CreateCommand();

sqlCommand.CommandText = DBWorker.MSSQLSelectQuery;

SqlDataAdapter sda = new SqlDataAdapter(sqlCommand.CommandText, connectionString);

SqlCommandBuilder scb = new SqlCommandBuilder(sda);

//Create a DataTable to hold the query results.
DataTable dTable = new DataTable();

//Fill the DataTable.
sda.Fill(dTable);

//BindingSource to sync DataTable and DataGridView.
BindingSource bSource = new BindingSource();

//Set the BindingSource DataSource.
bSource.DataSource = dTable;

//Set the DataGridView DataSource.
contentDataGridView.DataSource = bSource;
}
}
catch (SqlException e)
{
//Console.WriteLine(e.StackTrace);
}


sf_hussain786

Rimjhim4, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response