?
Solved

db connection with visual studio

Posted on 2005-03-10
12
Medium Priority
?
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month14 days, 6 hours left to enroll

770 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