how to retrieve data from database and display in grid view

Posted by Deepakji under C# on 1/28/2012 | Points: 10 | Views : 174506 | Status : [Member] | Replies : 5
hi,
i want to retrieve the data from database and display in grid view for windows application and after that i need to provide them option of download so i am not getting the functionality so please let me know urgent.




Responses

Posted by: Gow.net on: 1/29/2012 [Member] Starter | Points: 25

Up
0
Down
try this coding

try{


SqlConnection con = new SqlConnection("");//connection name
con.Open();
SqlCommand cmd = new SqlCommand("select * from tablename", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds, "ss");
dataGridView1.DataSource = ds.Tables["ss"]; ;
// dataGridView1.DataBind();
}
catch
{
MessageBox.Show("No Record Found");
}


gowthaman8870226416

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

Posted by: Arefin on: 1/29/2012 [Member] Starter | Points: 25

Up
0
Down
Okay.. Its good for downloading GridView data in Excel format. Just see the below links
http://www.codeproject.com/Tips/318636/Export-to-Excel-from-GridView-in-Csharp
http://www.dotnetfunda.com/articles/article1131-export-large-data-from-gridview-to-excel-file-using-csharp.aspx

Thanks,
Arefin

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

Posted by: Mandlaa on: 3/10/2013 [Member] Starter | Points: 25

Up
0
Down
I want to retrieve two values from database and store two variables
How can i read using data reader (or)dataset?

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

Posted by: Jayakumars on: 3/11/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
Mandlaa try this code

protected void bt1_Click(object sender, EventArgs e)
{
Dt = ReturnDt("Select StudentId,StudentName from dbo.Table_1"); // If U need 2 Values from Database and Retrieve Datatable
Dt = ReturnDt("Select * from dbo.Table_1");// If u need More than values from Database and Retrieve Datatable
}


public DataTable ReturnDt(string RecStr)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = RecStr;
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(Dt);
return Dt;
}

Mark as Answer if its helpful to you

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

Posted by: Raja_89 on: 3/12/2013 [Member] Starter | Points: 25

Up
0
Down
Hai


try
{

SqlConnection con = new SqlConnection("YOUR CONNECTION");
SqlCommand cmd = new SqlCommand("SQL STATEMENT", con);

con.Open();
to get the value based on selection
cmd.Parameters.AddWithValue("@Course_Name", Textbox1.Text);
SqlDataReader sdr = cmd.ExecuteReader();

while (sdr.Read())
{
string username;
Textbox2.Text= Convert.ToString(sdr.GetInt32 (sdr.GetOrdinal("field name")));
.....
or
username=Convert.ToString(sdr.GetInt32 (sdr.GetOrdinal("field name")));


}
}


catch (Exception ex)
{

Response.Write(ex.Message);


}

Regards


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

Login to post response