Click here to Skip to main content
15,867,835 members
Please Sign up or sign in to vote.
1.35/5 (5 votes)
See more:
how to get data from database to gridview by coding...........

my column are id, date_time and viewbooking

as in view booking i have created hyperlink to show data from another form.
Posted
Updated 13-Dec-18 0:45am
Comments
Arunprasath Natarajan 4-Sep-12 23:30pm    
New to .Net?
Member 10471582 30-Dec-13 23:54pm    
i have large database, using gridview control to display the all data from sql database, using c#.net and asp.net. but it produce system.out of memory exception. i give the code is
//code:
SqlConnection sqlcon = new SqlConnection("connection string");
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("select * from table", sqlcon);
//sqlcmd.ExecuteNonQuery();
SqlDataAdapter sqlda = new SqlDataAdapter(sqlcmd);
DataSet ds = new DataSet();
sqlda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();

how to resolve this ?..........
AmitGajjar 5-Sep-12 0:33am    
It doesn't matter if you are displaying String/DateTime.

Hi

try this code

C#
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");

            }
 
Share this answer
 
v2
See below code,
1. Read the records from the database to a datareader
2. then assign the datareader results to the gridview

In this example I am using Mysql to demonstrate. If you are using SQL Server. there is no much different except the you need to use respective classes.
 private void GetResults()
        {
            //Establishing the MySQL Connection
             MySqlConnection conn = new MySqlConnection("Database=potentiality_live;Data Source=eu;User Id=ptly;Password=phat40");

            string query;
            MySqlCommand SqlCommand;
            MySqlDataReader reader;

            MySqlDataAdapter adapter = new MySqlDataAdapter();
//Open the connection to db
            conn.Open();

//Generating the query to fetch the contact details
            query = "SELECT id,date_time,link FROM'sdfsdfsdf'";

 SqlCommand = new MySqlCommand(query, conn);
            adapter.SelectCommand = new MySqlCommand(query, conn);
//execute the query
            reader = SqlCommand.ExecuteReader();
//Assign the results 
            GridView1.DataSource = reader;

//Bind the data
            GridView1.DataBind();

}


Feel free to ask me if you have any further issues. If this solution is working then please click on the accept solution.

thank you
 
Share this answer
 
Comments
Gridi Kono 6-May-14 16:33pm    
i wanna display id in table sdfsdfsdf in a label and not in gridview. can u help me please?
Swinkaran 15-May-14 19:31pm    
Do you mean you want to use a <asp:label> in side a table?<br>
 <br>
If so, you need to use <asp:table>, which is a server side table and can add the <asp:label> control to this table,<br>
 <br>
protected void Page_Init(object sender, EventArgs e)<br>
{<br>
foreach (TableRow row in this.Table1.Rows)<br>
{<br>
foreach (TableCell cell in row.Cells)<br>
{<br>
// to add control<br>
Label lblID= new Label();<br>
lblID.Text = "Some Value";<br>
cell.Controls.Add(lblID);<br>
}<br>
}<br>
}
Muhammad Sayyam 20-May-14 6:15am    
HiI get the database data according to your solution but in my case if have to include a column which is not in database? This column is just for numeric number counting... Any suggestions?
Hi ,
Check this example
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = GetData();
        GridView1.DataBind();
    }

}
DataTable GetData()
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["testconection"].ConnectionString))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("select id , name from tableName ",con))
        {

            SqlDataAdapter adpt = new SqlDataAdapter(cmd);
            adpt.Fill(dt);
        }

    }
    return dt;
}

Best Regards
M.Mitwalli
 
Share this answer
 
Comments
__TR__ 5-Sep-12 3:31am    
+5
Mohamed Mitwalli 5-Sep-12 3:45am    
Thanks TR :)
C#
public partial class DesignationAddList : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection("Server=ServerName;User Id=sa;Password=sa123;Database=databasename");
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindData();
        }

    }
    private void BindData()
    {
        try
        {
            con.Open();
            SqlCommand com = new SqlCommand("select * from TableName", con);
            com.CommandType = CommandType.Text;
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds, "TableName");
            designationlistgrid.DataSource = ds.Tables["TableName"];
            designationlistgrid.DataBind();
            con.Close();
            lbldisplay.Text = "records are found";
        }
        catch
        {
            lbldisplay.Text = "records are not found";
        }
    }
   
}
 
Share this answer
 
v2
Comments
CHill60 19-Jun-13 8:47am    
Doesn't add much that wasn't in Solution 4 posted 10 months ago
VB
string query = "SELECT ChalanItem.ChalanId,ChalanItem.ProductId,CategoryMaster.CategoryId, CategoryMaster.CategoryName, ProductMaster.ProductName, ChalanItem.Qty"
      + "FROM  ChalanItem INNER JOIN " +
                     "CategoryMaster ON ChalanItem.CategoryId = CategoryMaster.CategoryId INNER JOIN"+
                    " ProductMaster ON ChalanItem.ProductId = ProductMaster.ProductId AND CategoryMaster.CategoryId = ProductMaster.CategoryId";
        gdvSendOrderDetails.DataSource=Helper.ExeCuteReader_return(query);



And a helper class with its methods as follows
C#
public static SqlDataReader ExeCuteReader_return(string query)
    {
        using (SqlConnection con = new SqlConnection(Connect.getConnectionSource()))
        {
            SqlCommand cmd = new SqlCommand(query, con);
            con.Open();
            try
            {

                SqlDataReader dr = cmd.ExecuteReader();
                return dr;
            }
            catch
            {
                throw;
            }
        }
    }
 
Share this answer
 
v2

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