Guest User

Guest User

  • Tech Writer
  • 2
  • 1.1k

display mysql database data in Tab control winforms c#

May 31 2019 8:59 PM
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?
 
  1. using System.Xml.Linq;  
  2. using MySql;  
  3. using System.Configuration;  
  4. using MySql.Data;  
  5. using System.Xml;  
  6. using System.IO;  
  7. using System.Runtime.InteropServices;  
  8. using Telerik.WinControls;  
  9. using Telerik.WinControls.UI;  
  10. using Telerik.WinControls.UI.Docking;  
  11. using System.Globalization;  
  12.   
  13. namespace mydatabase  
  14. {  
  15.     public partial class Form1 : Form  
  16.     {  
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }         
  21.   
  22.         private void Form1_Load(object sender, EventArgs e)  
  23.         {  
  24.             this.IsMdiContainer = true;  
  25.             string constring = "datasource=localhost;Port=3306;database=mydatabase;username=root;password=mypassword;persist security info=True";  
  26.             MySqlConnection MySqlConn = new MySqlConnection(constring);  
  27.             MySqlCommand command = new MySqlCommand("select * from mydatabase.Authors;", MySqlConn);  
  28.                  
  29.             try  
  30.             {  
  31.                   
  32.                 MySqlDataAdapter daAuthors = new MySqlDataAdapter();  
  33.                 daAuthors.SelectCommand = command;  
  34.                 DataTable dtAuthors = new DataTable();  
  35.                 DataSet dsAuthors = new DataSet();  
  36.                 daAuthors.Fill(dtAuthors);  
  37.                 dsAuthors.Tables.Add(dtAuthors);  
  38.         
  39.                 dtAuthors.Columns["AuthorID"].AutoIncrement = true;  
  40.                 dtAuthors.Columns[0].AutoIncrementStep = 1;  
  41.   
  42.                 // Bind the DataTable to the UI via a BindingSource.  
  43.   
  44.                 BindingSource AuthorBindingSource = new BindingSource();  
  45.   
  46.                 AuthorBindingSource.DataSource = dtAuthors;   // binding source binding  
  47.                 AuthorBindingNavigator.BindingSource = AuthorBindingSource; // Navigator Binding  
  48.   
  49.                 txtAuthorID.DataBindings.Add("Text", AuthorBindingSource, "AuthorID");  
  50.                 txtAuthorCode.DataBindings.Add("Text", AuthorBindingSource, "AuthorCode");  
  51.                 txtAuthorName.DataBindings.Add("Text", AuthorBindingSource, "AuthorName");  
  52.   
  53.                 // if it didn't find the key, position = 1  
  54.                 // you can also try any else proper event  
  55.   
  56.                   
  57.                 //AuthorBindingSource.Position = AuthorBindingSource.Find("AuthorID", Interaction.IIf(string.IsNullOrEmpty(txtAuthorID.Text), 0, txtAuthorID.Text));  
  58.   
  59.   
  60.             }  
  61.             catch (Exception ex)  
  62.             {  
  63.                 MessageBox.Show(ex.Message);  
  64.   
  65.                    
  66.                 string conBook = "datasource=localhost;Port=3306;database=mydatabase;userid=root;password=mypassword;persist security info=True";  
  67.                 MySqlConnection mySqlConnBook = new MySqlConnection(conBook);  
  68.                 MySqlCommand cmdBook = new MySqlCommand("SELECT * From mydatabase.Book", mySqlConnBook);  
  69.   
  70.                 MySqlDataAdapter daBook = new MySqlDataAdapter();  
  71.                 daBook.SelectCommand = cmdBook;  
  72.                 DataTable dtBook = new DataTable();  
  73.                 DataSet dsBook = new DataSet();  
  74.                 //  daBook.MissingSchemaAction = MissingSchemaAction.AddWithKey;  
  75.                 daBook.Fill(dtBook);  
  76.                 dsBook.Tables.Add(dtBook);  
  77.                // MySqlCommandBuilder cbBook = new MySqlCommandBuilder(daBook);  
  78.   
  79.                 dtBook.Columns["BookCodeID"].AutoIncrement = true;  
  80.                 dtBook.Columns[0].AutoIncrementStep = 1;  
  81.   
  82.                 // Bind the DataTable to the UI via a BindingSource.  
  83.   
  84.                 BindingSource BookBindingSource = new BindingSource();  
  85.   
  86.                 BookBindingSource.DataSource = dtBook;  
  87.                 BookBindingNavigator.BindingSource = BookBindingSource;  
  88.   
  89.                 txtBookCodeID.DataBindings.Add("Text", BookBindingSource, "BookCodeID");  
  90.                 txtBookDescription.DataBindings.Add("Text", BookBindingSource, "BookDescription");  
  91.             }  
  92.         }  
  93.   
  94.         private void next_radPageViewPage(RadPageViewPage radPageViewPage1, RadPageViewPage radPageViewPage2)  
  95.         {  
  96.             radPageView1.SelectedPage = radPageViewPage2;  
  97.             radPageViewPage2.BindingContextChanged += (_, __) =>  
  98.             radPageView1.SelectedPage = radPageViewPage1;  
  99.         }  
  100.   
  101.     }  
  102.  }  
 
 
 

Answers (1)