none
display mysql database data in Tab control winforms c# RRS feed

  • Question

  • 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;  
            }  
      
        }  
     }  


    Saturday, June 1, 2019 2:07 AM

Answers

  • Hello,

    First off you are asking more than one question, the first for working with tabs, the second for insert, update and delete. The rule of thumb is one post, one question.

    In regards to the overall code, I highly recommend first rethinking how you are handling errors, what's in the catch should never be there as the catch is to handle things that you did not count on and to recover gracefully. So consider a different strategy to view one or the other tables using conditional statements.

    How to work with tabs, first off think about the BindingSource components as anchors and that they are visible to any control on this form, work from them instead of attempting to jump between tabs as done in the event next_radPageViewPage. And lastly, you might consider asking advice in the following telerik support forum.

    https://www.telerik.com/support/winforms


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by wirejp Saturday, June 1, 2019 11:11 PM
    Saturday, June 1, 2019 10:23 AM
    Moderator