Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i write Program to display table values from the database..
i have a table in my database like...
eid,ename,address,designation,salary...
i want to display these values in Gridview..and before display table values i want to convert in XMl format, please any one help me regarding this...
Posted

1 Write stored procedure and bind that procedure with your grid view see below link for your reference:
http://www.codecomplete4u.com/showing-data-gridview-database-asp-net/[^]
 
Share this answer
 
public void BindGrid()
        {
            using (SqlConnection con = new SqlConnection("ConnectionString"))
            {
                SqlCommand cmd = new SqlCommand()
                {
                    Connection = con,
                    CommandText = "StoreProcedureName/sqlquery",
                    CommandType = CommandType.StoredProcedure//if your commandtext containts SP Name else use CommandType.Text
                };
                //if required add parameter like cmd.Parameters.Add
                cmd.Connection.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                DataTable _dt = new DataTable();
                _dt.Load(rdr);
                //before binding your gridview load xml file from DataTable
                System.IO.StringWriter _writer = new System.IO.StringWriter();
                _dt.WriteXml(_writer, XmlWriteMode.WriteSchema, false);
                string _result = _writer.ToString();
                //'_gridview' is id of your Gridview
                _gridview.DataSource = _dt;
                _gridview.DataBind();
            }
        }
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 2-Dec-13 9:58am    
nice, good comments for easy understanding..
Kumar Kovuru 2-Dec-13 23:05pm    
Hi Anupkumar,
Pls explain Clearly...because am new to asp.net.....
This works if any issue dont forget to check your connection string


C#
SqlConnection Conn = new SqlConnection("YourConnectionString");
SqlDataReader rdr = null;
string commandString = "SELECT * FROM YourTableName";

try
{
       Conn.Open();
       SqlCommand Cmd = new SqlCommand(commandString, Conn);
       rdr = Cmd.ExecuteReader();

       YourGridId.DataSource = rdr;
       YourGridId.DataBind();
}
catch (Exception ex)
{
     // Log error
}
finally
{
    if (rdr != null)
    {
        rdr.Close();
    }
    if (Conn != null)
    {
        Conn.Close();
    }
 }
 
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