1- تو وب کانفیگ کانکشن استرینگ
g
<add name="STNMap" connectionString="server=localhost ;Port=3306;database=stnmap; Uid=root ;Password=123" providerName="MySql.Data.MySqlClient"/>;
2- کد برنامه در صفحه main
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.MySqlClient;
using System.Web.Configuration;
public partial class Main : System.Web.UI.Page
{
string strCon = WebConfigurationManager.ConnectionStrings["STNMap"].ConnectionString;
MySqlConnection conn = null;
MySqlCommand cmd = null;
MySqlDataReader dtr = null;
static DataTable dtblBrand = new DataTable();
DataColumn dcolBrandNo = new DataColumn("intBrandNo");
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection(strCon);
if (!IsPostBack)
{
dtblBrand.Rows.Clear();
dtblBrand.Columns.Clear();
dtblBrand.Columns.Add(dcolBrandNo);
BindlstCategory(conn);
lstCategory.SelectedIndex = 0;
BindBrands();
if (gwBrands.Rows.Count != 0)
{
int intBrandNo = Convert.ToInt32(gwBrands.DataKeys[0].Value);
BindMobileModels(intBrandNo);
}
}
}
protected void BindBrands()
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM tbl_Brands", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
// cmd.Connection.Open();
dtr = cmd.ExecuteReader();
gwBrands.DataSource = dtr;
gwBrands.DataBind();
dtr.Close();
conn.Close();
}
protected void BindMobileModels(int intBrandNo)
{
MySqlCommand cmd = new MySqlCommand("SELECT * FROM tbl_Map WHERE intBrandNo=@intBrandNo ", conn);
cmd.CommandType = CommandType.Text;
MySqlParameter parmBrandNo = new MySqlParameter("@intBrandNo", intBrandNo);
cmd.Parameters.Add(parmBrandNo);
conn.Open();
// cmd.Connection.Open();
dtr = cmd.ExecuteReader();
gwMobiles.DataSource = dtr;
gwMobiles.DataBind();
dtr.Close();
conn.Close();
}
protected void gwBrands_RowCommand(object sender, GridViewCommandEventArgs e)
{
int intBrandNo = Convert.ToInt32(e.CommandArgument);
BindMobileModels(intBrandNo);
}
protected string Image(string name)
{
return "./Image/" + name;
}
protected void btnSearch_Click(object sender, EventArgs e)
{
MySqlCommand cmd = new MySqlCommand("Select * FROM tbl_Brands WHERE intCategoryID=@intCategoryID ", conn);
cmd.CommandType = CommandType.Text;
MySqlParameter parmCategoryNo = new MySqlParameter("@intCategoryID", Convert.ToInt32(lstCategory.SelectedValue));
cmd.Parameters.Add(parmCategoryNo);
conn.Open();
// cmd.Connection.Open();
dtr = cmd.ExecuteReader();
gwBrands.DataSource = dtr;
gwBrands.DataBind();
dtr.Close();
conn.Close();
}
private void BindlstCategory(MySqlConnection conn)
{
MySqlCommand cmd = new MySqlCommand("Select * FROM tbl_Category", conn);
cmd.CommandType = CommandType.Text;
conn.Open();
// cmd.Connection.Open();
dtr = cmd.ExecuteReader();
lstCategory.DataSource = dtr;
lstCategory.DataTextField = "nvcCategoryName";
lstCategory.DataValueField = "intCategoryID";
lstCategory.DataBind();
dtr.Close();
conn.Close();
}
}
H