Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

db connection with visual studio

Posted on 2005-03-10
12
Medium Priority
?
481 Views
Last Modified: 2010-08-05
hi
Im using visual studio and i want to display the contents of a db on a windows form.

I go to
file
new
project
windows application

and then for the code i put

#region Using directives

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

#endregion

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

    public static void Main(string[] args)
    {
        SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
        SqlConn.Open();
        SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            MessageBox.Show(reader["page_id"].ToString());
        }
        SqlConn.Close();
    }


    }
}

but its giving me errors

      Error      1            'WindowsApplication11.Form1.Dispose(bool)': no suitable method found to override      C:\Documents and Settings\paulwhelan.VIZOR\My Documents\Visual Studio\Projects\WindowsApplication11\WindowsApplication11\Form1.Designer.cs      13      32                  


and its also saying it has 'more than one entry point defined'

Any ideas?
I'm a total newbie (in case its not obvious!)
Thanks
AM
0
Comment
Question by:amoran
  • 6
  • 4
  • 2
12 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 13504839
Create application again using Wizard and paste Main function code to it. I suggest you to create Console application, since this code doesn't use form:

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
    class Class1
    {
        [STAThread]
        static void Main(string[] args)
        {
            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["page_id"].ToString());
            }
            SqlConn.Close();
        }
    }
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13504865
Your code is missing some functions required by Windows Forms application. This is the way to do the same in Windows Forms application (form itself is not used):

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace WindowsApplication10
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void Form1_Load(object sender, System.EventArgs e)
        {
            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["page_id"].ToString());
            }
            SqlConn.Close();
        }
    }
}
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13504871
Correction: in the second post line
Console.WriteLine(reader["page_id"].ToString());
should be replaced with:
MessageBox.Show(reader["page_id"].ToString());
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:amoran
ID: 13504930
ok
in my form1.cs i put the code in your second post

now it says

      Error      2            The namespace 'WindowsApplication10' already contains a definition for 'Form1'      C:\\Visual Studio\Projects\WindowsApplication12\WindowsApplication12\Form1.Designer.cs      14      17                  
      Warning      1            Cannot open a designer for the file because the class within it does not inherit from a class that can be visually designed.            0      -1                  


thanks!
AM
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13504949
Create application again and after this paste this code replacing all existing code in the file.
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 13505146
Check this...

http://www.connectionstrings.com/

You will get the complete list of connection strings with the code.

A must check ;)

sun4sunday
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 13505160
Also you have to use this for the connection

using System.Data.SqlClient;

sun4sunday
0
 

Author Comment

by:amoran
ID: 13505194
ok now i do this


#region Using directives

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

#endregion

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

        private void button1_Click(object sender, EventArgs e)
        {

            SqlConnection SqlConn = new SqlConnection("server=localhost;DataBase=pubs;uid=sa;pwd=sa");
            SqlConn.Open();
            SqlCommand cmd = new SqlCommand("dbcc extentinfo ('pubs','authors',1)", SqlConn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                MessageBox.Show(reader["page_id"].ToString());
            }
            SqlConn.Close();


        }
    }
}

but i get (in reference to SqlConn.Open();)
sqlexception was unhandled

any ideas?
thanks for the help
AM
0
 

Author Comment

by:amoran
ID: 13505241
I think it can't find the pubs database?
Is it on the web?
Thanks
AM
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 13505318
According to connection string, SQL server shoul be installed on your computer: server=localhost. If you are working with XP, you cannot install SQL server and need to install MSDE which is available for download from Microsoft WEB site.
0
 

Author Comment

by:amoran
ID: 13505690
cool
I installed msde
now when i run it i get

Named Pipes Provider: The network path was not found.\r\n

Which I guess is a step in the right direction!
Thanks for the help
AM
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 400 total points
ID: 13505808
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question