About Me

My photo
Dhaka, Bangladesh
I am B.S.C Engineer,CSE,SUST and Ex-Cadet of Mirzapur Cadet College.

Saturday, July 5, 2008

DATA GRID & MySQL

Remove -- after "<" Symbol HTML/asp: <--asp:GridView ID="MySQLDataGrid" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="product_id" EnableSortingAndPagingCallbacks="True" runat="server" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None" Width="802px"-->
<--Columns>

<--asp:HyperLinkField DataTextField="product_id" HeaderText="product_id" SortExpression="product_id"/-->
<--asp:BoundField DataField="name" HeaderText="name" SortExpression="name" /-->
<--asp:BoundField DataField="description" HeaderText="description" SortExpression="description" /-->
<--asp:BoundField DataField="lifecycle" HeaderText="lifecycle" SortExpression="lifecycle" /-->
<--asp:BoundField DataField="price" HeaderText="price" SortExpression="price" /-->
<--asp:HyperLinkField HeaderText="Subscribe" DataNavigateUrlFields="page_url" Text="Subscribe"/-->


<--/Columns>
<--FooterStyle BackColor="#C6C3C6" ForeColor="Black" /-->
<--RowStyle BackColor="#DEDFDE" ForeColor="Black" /-->
<--PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" /-->
<--SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" /-->
<--HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" /-->
<--PagerSettings Mode="NumericFirstLast" /-->
<--/asp:GridView>




C#(aspx.cs):

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
//using MySql.Data;
//using MySql.Data.MySqlClient;

public partial class Products : System.Web.UI.Page
{
System.Data.Odbc.OdbcConnection myConn;
//System.Data.Odbc.OdbcDataReader MyReader = null;
System.Data.Odbc.OdbcDataAdapter myDataAdapter;
DataSet myDataSet;

protected void Page_Load(object sender, EventArgs e)
{
//MySqlConnection myConnection;
//MySqlDataAdapter myDataAdapter;

// String strSQL;
//String MySQLConnectionString;

//MySQLConnectionString = "Server=localhost;" + "Database=KrisanCust;" + "Uid=root;" + "Pwd=t;" + "Connect Timeout=30;";
//MySQLConnectionString = "Server=p50mysql99.secureserver.net;" + "Database=KrisanCust;" + "Uid=KrisanCust;" + "Pwd=;" + "Connect Timeout=30;";

//myConnection = new MySqlConnection(MySQLConnectionString);



//strSQL = "SELECT product_id, name, description, lifecycle, price FROM tbl_product";



//-------------------------------Alternative---------------------//

try
{
//myConn = new System.Data.Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=localhost;;Database=krisancust;User=root;Password=linuxhost;");
myConn = new System.Data.Odbc.OdbcConnection("Driver={MySQL ODBC 3.51 Driver};Server=p50mysql99.secureserver.net;PORT=3306;Database=KrisanCust;User=;Password=;OPTION=0;");
myConn.Open();

string sql = "SELECT product_id, name, description, lifecycle, price,page_url FROM tbl_product";
//System.Data.Odbc.OdbcCommand cmd1 = new System.Data.Odbc.OdbcCommand(sql, myConn);
//Response.Write(sql);
//MyReader = cmd1.ExecuteReader();
//MyReader.Close();

myDataAdapter = new System.Data.Odbc.OdbcDataAdapter(sql, myConn);
myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet, "tbl_product");
MySQLDataGrid.DataSource = myDataSet.Tables["tbl_product"].DefaultView;
MySQLDataGrid.DataBind();

//Response.Write(MySQLDataGrid.Rows[0].Cells[0].ToString());

}
catch (Exception ee)
{
Response.Write("Mysql Error" + ee);
}
finally
{
myConn.Close();

}

//-------------------------------Finishing Alternative-----------//

}



}

No comments: