display mysql database data in Tab control winforms c#

  • Thread starter Thread starter wirejp
  • Start date Start date
W

wirejp

Guest
I have a mysql database linked to a tab control with multiple tables. I got the first table display in one tab page. But I cannot get the data to be displayed into another tab control. How do I do this? Also What is the code to perform the insert, update and delete the data?


using System.Xml.Linq;
using MySql;
using System.Configuration;
using MySql.Data;
using System.Xml;
using System.IO;
using System.Runtime.InteropServices;
using Telerik.WinControls;
using Telerik.WinControls.UI;
using Telerik.WinControls.UI.Docking;
using System.Globalization;

namespace mydatabase
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
string constring = "datasource=localhost;Port=3306;database=mydatabase;username=root;password=mypassword;persist security info=True";
MySqlConnection MySqlConn = new MySqlConnection(constring);
MySqlCommand command = new MySqlCommand("select * from mydatabase.Authors;", MySqlConn);

try
{

MySqlDataAdapter daAuthors = new MySqlDataAdapter();
daAuthors.SelectCommand = command;
DataTable dtAuthors = new DataTable();
DataSet dsAuthors = new DataSet();
daAuthors.Fill(dtAuthors);
dsAuthors.Tables.Add(dtAuthors);

dtAuthors.Columns["AuthorID"].AutoIncrement = true;
dtAuthors.Columns[0].AutoIncrementStep = 1;

// Bind the DataTable to the UI via a BindingSource.

BindingSource AuthorBindingSource = new BindingSource();

AuthorBindingSource.DataSource = dtAuthors; // binding source binding
AuthorBindingNavigator.BindingSource = AuthorBindingSource; // Navigator Binding

txtAuthorID.DataBindings.Add("Text", AuthorBindingSource, "AuthorID");
txtAuthorCode.DataBindings.Add("Text", AuthorBindingSource, "AuthorCode");
txtAuthorName.DataBindings.Add("Text", AuthorBindingSource, "AuthorName");

// if it didn't find the key, position = 1
// you can also try any else proper event


//AuthorBindingSource.Position = AuthorBindingSource.Find("AuthorID", Interaction.IIf(string.IsNullOrEmpty(txtAuthorID.Text), 0, txtAuthorID.Text));


}
catch (Exception ex)
{
MessageBox.Show(ex.Message);


string conBook = "datasource=localhost;Port=3306;database=mydatabase;userid=root;password=mypassword;persist security info=True";
MySqlConnection mySqlConnBook = new MySqlConnection(conBook);
MySqlCommand cmdBook = new MySqlCommand("SELECT * From mydatabase.Book", mySqlConnBook);

MySqlDataAdapter daBook = new MySqlDataAdapter();
daBook.SelectCommand = cmdBook;
DataTable dtBook = new DataTable();
DataSet dsBook = new DataSet();
// daBook.MissingSchemaAction = MissingSchemaAction.AddWithKey;
daBook.Fill(dtBook);
dsBook.Tables.Add(dtBook);
// MySqlCommandBuilder cbBook = new MySqlCommandBuilder(daBook);

dtBook.Columns["BookCodeID"].AutoIncrement = true;
dtBook.Columns[0].AutoIncrementStep = 1;

// Bind the DataTable to the UI via a BindingSource.

BindingSource BookBindingSource = new BindingSource();

BookBindingSource.DataSource = dtBook;
BookBindingNavigator.BindingSource = BookBindingSource;

txtBookCodeID.DataBindings.Add("Text", BookBindingSource, "BookCodeID");
txtBookDescription.DataBindings.Add("Text", BookBindingSource, "BookDescription");
}
}

private void next_radPageViewPage(RadPageViewPage radPageViewPage1, RadPageViewPage radPageViewPage2)
{
radPageView1.SelectedPage = radPageViewPage2;
radPageViewPage2.BindingContextChanged += (_, __) =>
radPageView1.SelectedPage = radPageViewPage1;
}

}
}

Continue reading...
 
Back
Top