How to associate mysql data to DataRepeater controls - c#

I have a DataRepeater template control with 2 textboxes which I want to associate data from a mysql table
When I run this program I get the representation on my DataRepeater the number of rows existing in my mysql table
RESULT
my question is how to associate the data to those textboxes
private void Form1_Load(object sender, EventArgs e)
{
BindLviewData();
}
protected void BindLviewData()
{
System.Data.DataTable dt = new System.Data.DataTable("db.myTable");
MySqlConnection dbConn = new MySqlConnection("Server = localhost; Database = db; Uid = wwww; Pwd = www; ");
dbConn.Open();
string query = string.Format("SELECT Col1,Col2 FROM myTable");
MySqlCommand cmd = new MySqlCommand(query, dbConn);
MySqlDataAdapter returnVal = new MySqlDataAdapter(cmd);
returnVal.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataRepeater1.DataSource = bs;
dbConn.Close();
}

Related

C# How to show Database table's items in datagridview (SQL)

I have two combobxes and datagridview,in first combobox has written my database's names,when i select any database's name, in second combobox appears that database table's name.I need when i select any table, it must show me that table's items in datagridview.For example i select Suren_Products database, in second combobox appears 3 tables' names,from there i select products table, then in datagridview must show me that table's items.
But in this case Whatever I select,in datagridview shows me only products items.Why?
public partial class Form1 : Form
{
Model db;
public SqlConnection conn;
public SqlDataAdapter adapter;
public DataSet ds;
string connStr = ConfigurationManager.ConnectionStrings["default"].ConnectionString;
public Form1()
{
InitializeComponent();
this.db = new Model();
DataSet ds = db.Get_Databases();
cb1.DataSource = ds.Tables[0];
cb1.DisplayMember = "Database_Name";
}
private void cb1_SelectedIndexChanged(object sender, EventArgs e)
{
string x = (cb1.SelectedItem as DataRowView).Row["Database_Name"].ToString();
DataSet ds = cb1.SelectedItem as DataSet;
ds = db.Get_Tables(x);
cb2.DataSource = ds.Tables[0];
cb2.DisplayMember = "name";
}
private void cb2_SelectedIndexChanged(object sender, EventArgs e)
{
conn = new SqlConnection(connStr);
adapter = new SqlDataAdapter("select * from Products", connStr);
ds = new DataSet();
adapter.Fill(ds);
dgv.DataSource = ds.Tables[0];
}
}

Reuse DataTable in ButtonClick which is already open in form load

I have open VFP dbf file from formload got the data into data table and
this also in gridview, I want to use this data result in another button click
How can I call that same data in button click,I am new to this hope this maybe
very easy for some , Please help me.
private void Form1_Load(object sender, EventArgs e)
{
DataTable YourResultSet = new DataTable();
OleDbConnection yourConnectionHandler = new OleDbConnection(
#"Provider=VFPOLEDB.1;Data Source=I:\\Bestall\\T&AData.dbc");
yourConnectionHandler.Open();
if (yourConnectionHandler.State == ConnectionState.Open)
{
string mySQL = "select * from Findat"; // dbf table name
OleDbCommand MyQuery = new OleDbCommand(mySQL, yourConnectionHandler);
OleDbDataAdapter DA = new OleDbDataAdapter(MyQuery);
DA.Fill(YourResultSet);
dataGridView1.DataSource = YourResultSet.DefaultView;
// yourConnectionHandler.Close();
}
}

Binding Combobox with database

I have combobox in my form which display value from database. I want one default value so I use .text property also and on selected index changed event the label display the corresponding value of the database the code is not working
string cstr = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;
private void AddStock_Load(object sender, EventArgs e)
{
bindcombo();
}
private void bindcombo()
{
SqlConnection con = new SqlConnection(cstr);
SqlDataAdapter da = new SqlDataAdapter("SELECT Dname FROM dealer", con);
DataTable dt = new DataTable();
da.Fill(dt);
cmbdealer.DataSource = dt;
cmbdealer.DisplayMember = "Dname";
cmbdealer.Text = "select-Dealer";
con.Close();
}
private void cmbdealer_SelectedIndexChanged(object sender, EventArgs e)
{
dealerdetails();
}
private void dealerdetails()
{
SqlConnection con = new SqlConnection(cstr);
SqlCommand cmd = new SqlCommand("select * from dealer where Dname='" + cmbdealer.Text + "'", con);
con.Open();
SqlDataReader re = cmd.ExecuteReader();
while (re.Read())
{
lbdl.Text = re["Ddlno"].ToString();
lbgst.Text = re["Dgstno"].ToString();
lbadd.Text = re["Daddress"].ToString();
}
}
the above code is working fine but when the form is load all the 3 label shows the database value of the first entry of my dealer table without selecting and value from combobox.

Trouble With Displaying Data in DataGridView

I am trying to display my mysql database table on a DataGridView using c# . but nothings happening . please tell me where i've gone wrong in my code
private void button1_Click(object sender, EventArgs e)
{
MySqlConnection conn = new MySqlConnection("Server = localhost; Database=project; user=root;password=''");
conn.Open();
string query = "SELECT * FROM billing";
MySqlDataAdapter bills = new MySqlDataAdapter(query, conn);
MySqlCommandBuilder cbuilder = new MySqlCommandBuilder(bills);
DataTable dtable = new DataTable();
bills.Fill(dtable);
//the DataGridView
DataGridView dgView = new DataGridView();
//BindingSource to sync DataTable and DataGridView
BindingSource bSource = new BindingSource();
//set the BindingSource DataSource
bSource.DataSource = dtable;
//set the DataGridView DataSource
dgView.DataSource = bSource;
bills.Update(dtable);
}

Grid View in Asp.net C# with wamp Server's MYSQL

I want to show my data in grid view in my Asp.new c# form.
I am using Wamp Server's MySQL database.
I have tired a lot for binding database with grid view.
Please do help.
my code :
public partial class Temp : System.Web.UI.Page
{
string conString = ConfigurationManager.ConnectionStrings["AASProject"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
MySqlConnection con = new MySqlConnection(conString);
con.Open();
gvFoodDetail.RowEditing+= new GridViewEditEventHandler(gvFoodDetail_RowEditing);
gvFoodDetail.RowDeleting += new GridViewDeleteEventHandler(gvFoodDetail_RowDeleting);
gvFoodDetail.RowUpdating += new GridViewUpdateEventHandler(gvFoodDetail_RowUpdating);
//gvFoodDetail_RowCancelingEdit += new GridViewCancelEditEventArgs(gv);
// gvFoodDetail.RowEditing += new GridViewEditEventHandler(gvFoodDetail_RowEditing);
// gvFoodDetail.RowDeleting += new GridViewDeleteEventHandler(gvFoodDetail_RowDeleting);
// gvFoodDetail.RowUpdating += new GridViewUpdateEventHandler(gvFoodDetail_RowUpdating);
// gvFoodDetail.RowCancelingEdit += new GridViewCancelEditEventHandler(gvFoodDetail_RowCancelingEdit);
gvFoodDetail.PageIndexChanging += new GridViewPageEventHandler(gvFoodDetail_PageIndexChanging);
if (!IsPostBack)
{
gvFoodDetail.Visible = true;
loadFoodDB();
}
}
public void loadFoodDB()
{
string getFoodDetails = "Select Emp_ID,intime,outtime,date From datetime";
MySqlConnection con = new MySqlConnection(conString);
con.Open();
MySqlCommand cmd = new MySqlCommand(getFoodDetails, con);
DataTable dt = new DataTable();
MySqlDataAdapter da = new MySqlDataAdapter(cmd);
da.Fill(dt);
con.Close();
gvFoodDetail.DataSource = dt;
gvFoodDetail.DataBind();
}
datagridview.Datasource = datasource
datagridview.databind()
This should set and show the data. You have to handle the columns and everything manually or automatically.

Categories