Skip to content

C# newbie-error with program.cs and possible issue with DataRelation on many-to-many relationship

D Rhoades

Asked Feb 27, 2009, 6:17 AM EST1 answer
Share
LinkedInX

This is my first C# project and I'm building an application to track books I've read and want to read. Here's the code in my datahandling class. I am getting a Debug error on Application.Run that says "Starting a second message loop on a single thread is not a valid operation. Use Form.ShowDialog instead. Is this error truly about that line of the code or about my class?


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace BooksAuthors
{
    static class Program
    {
         [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}


ClassDataHandling.cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BooksAuthors
{
    internal class ClassDataHandling
    {
        //BooksAuthors.ClassDataHandling _sqlData;

        public void MainDataClass()
        {
            //specify SQL connection string
            string connString = @"Server=RHOADESD;Integrated Security=True;" +
                "Database=BOOKS";
            SqlConnection conn = new SqlConnection(connString);
           

            SqlDataAdapter booksAdapter = new SqlDataAdapter(
                "Select * from books", conn);
            SqlDataAdapter bkauthlinkAdapter = new SqlDataAdapter(
                "Select * from bkauthlink", conn);
            SqlDataAdapter authorAdapter = new SqlDataAdapter(
                "Select * from authors", conn);

            conn.Open();

                      
            //Create DataSet to contain related data tables, rows and columns
            DataSet BookAuthDataSet = new DataSet("BookAuthors");
            booksAdapter.Fill(BookAuthDataSet, "books");
            bkauthlinkAdapter.Fill(BookAuthDataSet, "bkauthlink");
            authorAdapter.Fill(BookAuthDataSet, "authors");

            conn.Close();

            DataRelation book = BookAuthDataSet.Relations.Add(
                "BookRel", BookAuthDataSet.Tables["books"].Columns["booksID"],
                BookAuthDataSet.Tables["bkauthlink"].Columns["booksID"]);
            DataRelation author = BookAuthDataSet.Relations.Add(
                "AuthRel", BookAuthDataSet.Tables["authors"].Columns["authorsID"],
                BookAuthDataSet.Tables["bkauthlink"].Columns["authorsID"]);

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    I believe it is a problem with the class. I changed the program.cs to this:

    C#
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    namespace BooksAuthors
    {
    static class Program
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
    }
    }
    }

    and on the last line the debugger stops with "Child list for field Authors cannot be created.".

    0