CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2005
    Posts
    21

    Please help with SQL Server Express 5

    Can someone please help me with the following problem. I am trying to create a program in .NEt 2003 which connects to a database table in SQL server Express 5. However, after connecting to the local server, I then try to run the program and when I try to view the table in SQL express I get a 'System.NullreferenceException' and when I view the debug details it says that 'object reference is not set to an instance of an object' which am uncertain how to correct.

    I have added a connection object which is initialsied with the address of the datable in SQL. Please find below the code that I am using showing the initialised connection object and also the table being called in 'button6' control.

    I would greatly appreciate any help....thanks

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

    namespace MovieBase
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.Button button5;
    private System.Data.SqlClient.SqlConnection sqlConnection1;
    private System.Windows.Forms.ComboBox GengreList;
    private System.Windows.Forms.Button button6;
    private System.Windows.Forms.Button button7;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
    private System.Windows.Forms.Label label2;
    public DataSet MDSet = new DataSet();
    SqlDataAdapter MyAdapt;
    private System.Data.SqlClient.SqlCommand sqlSelectCommand2;
    private System.Data.SqlClient.SqlCommand sqlInsertCommand2;
    private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter2;
    //Connection object being initialised
    public SqlConnection MovCon = new SqlConnection("Data Source = SHERMAN/SQLEXPRESS;Initial Catalog= MovieDBase;User Id=myUsername;Password=myPassword");






    /// <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()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.label1 = new System.Windows.Forms.Label();
    this.button4 = new System.Windows.Forms.Button();
    this.button5 = new System.Windows.Forms.Button();
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    this.GengreList = new System.Windows.Forms.ComboBox();
    this.button6 = new System.Windows.Forms.Button();
    this.button7 = new System.Windows.Forms.Button();
    this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
    this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
    this.label2 = new System.Windows.Forms.Label();
    this.sqlSelectCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlInsertCommand2 = new System.Data.SqlClient.SqlCommand();
    this.sqlDataAdapter2 = new System.Data.SqlClient.SqlDataAdapter();
    this.SuspendLayout();
    //
    // button1
    //
    this.button1.Location = new System.Drawing.Point(184, 168);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(96, 23);
    this.button1.TabIndex = 0;
    this.button1.Text = "Add A Movie";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    //
    // button2
    //
    this.button2.Location = new System.Drawing.Point(184, 248);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(96, 23);
    this.button2.TabIndex = 1;
    this.button2.Text = "Delete A Movie";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    //
    // button3
    //
    this.button3.Location = new System.Drawing.Point(184, 208);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(96, 23);
    this.button3.TabIndex = 2;
    this.button3.Text = "View Movies";
    this.button3.Click += new System.EventHandler(this.button3_Click);
    //
    // label1
    //
    this.label1.Font = new System.Drawing.Font("Myriad Web Pro", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.label1.Location = new System.Drawing.Point(280, 8);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(120, 24);
    this.label1.TabIndex = 3;
    this.label1.Text = "MOVIEBASE";
    //
    // button4
    //
    this.button4.Location = new System.Drawing.Point(448, 344);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(192, 23);
    this.button4.TabIndex = 4;
    this.button4.Text = "Current Movie Count";
    this.button4.Click += new System.EventHandler(this.button4_Click);
    //
    // button5
    //
    this.button5.Location = new System.Drawing.Point(496, 112);
    this.button5.Name = "button5";
    this.button5.Size = new System.Drawing.Size(128, 32);
    this.button5.TabIndex = 5;
    this.button5.Text = "Find Movie";
    this.button5.Click += new System.EventHandler(this.button5_Click);
    //
    // sqlConnection1
    //
    this.sqlConnection1.ConnectionString = "workstation id=SHERMAN;packet size=4096;integrated security=SSPI;data source=\"SHE" +
    "RMAN\\SQLEXPRESS\";persist security info=False;initial catalog=MovieDBase";
    this.sqlConnection1.InfoMessage += new System.Data.SqlClient.SqlInfoMessageEventHandler(this.sqlConnection1_InfoMessage);
    //
    // GengreList
    //
    this.GengreList.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
    this.GengreList.Items.AddRange(new object[] {
    "Horror",
    "Comedy",
    "Family/Entertainment",
    "Drama",
    "Boxsets",
    "Sci -fi/Fantasy"});
    this.GengreList.Location = new System.Drawing.Point(184, 344);
    this.GengreList.Name = "GengreList";
    this.GengreList.Size = new System.Drawing.Size(121, 21);
    this.GengreList.TabIndex = 6;
    //
    // button6
    //
    this.button6.Location = new System.Drawing.Point(264, 104);
    this.button6.Name = "button6";
    this.button6.Size = new System.Drawing.Size(160, 40);
    this.button6.TabIndex = 7;
    this.button6.Text = "View Movie List";
    this.button6.Click += new System.EventHandler(this.button6_Click);
    //
    // button7
    //
    this.button7.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.button7.Location = new System.Drawing.Point(360, 432);
    this.button7.Name = "button7";
    this.button7.Size = new System.Drawing.Size(104, 40);
    this.button7.TabIndex = 8;
    this.button7.Text = "Exit Movie Base";
    this.button7.Click += new System.EventHandler(this.button7_Click);
    //
    // sqlSelectCommand1
    //
    this.sqlSelectCommand1.CommandText = "SELECT [Item Number], [Movie Title], Genre, [Date of Release], Director FROM [Mov" +
    "Table 1]";
    this.sqlSelectCommand1.Connection = this.sqlConnection1;
    //
    // sqlInsertCommand1
    //
    this.sqlInsertCommand1.CommandText = "INSERT INTO [MovTable 1] ([Item Number], [Movie Title], Genre, [Date of Release]," +
    " Director) VALUES (@Param1, @Param2, @Genre, @Param3, @Director); SELECT [Item N" +
    "umber], [Movie Title], Genre, [Date of Release], Director FROM [MovTable 1]";
    this.sqlInsertCommand1.Connection = this.sqlConnection1;
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.VarChar, 50, "Item Number"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.VarChar, 50, "Movie Title"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Genre", System.Data.SqlDbType.VarChar, 50, "Genre"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 50, "Date of Release"));
    this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Director", System.Data.SqlDbType.VarChar, 50, "Director"));
    //
    // sqlDataAdapter1
    //
    this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
    this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
    this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
    new System.Data.Common.DataTableMapping("Table", "MovTable 1", new System.Data.Common.DataColumnMapping[] {
    new System.Data.Common.DataColumnMapping("Item Number", "Item Number"),
    new System.Data.Common.DataColumnMapping("Movie Title", "Movie Title"),
    new System.Data.Common.DataColumnMapping("Genre", "Genre"),
    new System.Data.Common.DataColumnMapping("Date of Release", "Date of Release"),
    new System.Data.Common.DataColumnMapping("Director", "Director")})});
    this.sqlDataAdapter1.RowUpdated += new System.Data.SqlClient.SqlRowUpdatedEventHandler(this.sqlDataAdapter1_RowUpdated);
    //
    // label2
    //
    this.label2.Font = new System.Drawing.Font("Myriad Web Pro", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
    this.label2.Location = new System.Drawing.Point(64, 344);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(120, 23);
    this.label2.TabIndex = 9;
    this.label2.Text = "Genres";
    //
    // sqlSelectCommand2
    //
    this.sqlSelectCommand2.CommandText = "SELECT [Item Number], [Movie Title], Genre, [Date of Release], Director FROM [Mov" +
    "Table 1]";
    this.sqlSelectCommand2.Connection = this.sqlConnection1;
    //
    // sqlInsertCommand2
    //
    this.sqlInsertCommand2.CommandText = "INSERT INTO [MovTable 1] ([Item Number], [Movie Title], Genre, [Date of Release]," +
    " Director) VALUES (@Param1, @Param2, @Genre, @Param3, @Director); SELECT [Item N" +
    "umber], [Movie Title], Genre, [Date of Release], Director FROM [MovTable 1]";
    this.sqlInsertCommand2.Connection = this.sqlConnection1;
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param1", System.Data.SqlDbType.VarChar, 50, "Item Number"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param2", System.Data.SqlDbType.VarChar, 50, "Movie Title"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Genre", System.Data.SqlDbType.VarChar, 50, "Genre"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Param3", System.Data.SqlDbType.VarChar, 50, "Date of Release"));
    this.sqlInsertCommand2.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Director", System.Data.SqlDbType.VarChar, 50, "Director"));
    //
    // sqlDataAdapter2
    //
    this.sqlDataAdapter2.InsertCommand = this.sqlInsertCommand2;
    this.sqlDataAdapter2.SelectCommand = this.sqlSelectCommand2;
    this.sqlDataAdapter2.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
    new System.Data.Common.DataTableMapping("Table", "MovTable 1", new System.Data.Common.DataColumnMapping[] {
    new System.Data.Common.DataColumnMapping("Item Number", "Item Number"),
    new System.Data.Common.DataColumnMapping("Movie Title", "Movie Title"),
    new System.Data.Common.DataColumnMapping("Genre", "Genre"),
    new System.Data.Common.DataColumnMapping("Date of Release", "Date of Release"),
    new System.Data.Common.DataColumnMapping("Director", "Director")})});
    //
    // Form1
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.BackColor = System.Drawing.SystemColors.InactiveCaption;
    this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
    this.ClientSize = new System.Drawing.Size(704, 502);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.button7);
    this.Controls.Add(this.button6);
    this.Controls.Add(this.GengreList);
    this.Controls.Add(this.button5);
    this.Controls.Add(this.button4);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false);

    }
    #endregion

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

    }

    private void sqlConnection1_InfoMessage(object sender, System.Data.SqlClient.SqlInfoMessageEventArgs e)
    {

    }

    private void button5_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("///Under Construction");
    }

    private void button1_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("///Under Construction");
    }

    private void button2_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("///Under Construction");
    }

    private void button3_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("///Under Construction");
    }

    private void button4_Click(object sender, System.EventArgs e)
    {
    MessageBox.Show("There is currently no movie count available");
    }
    //button6_control
    private void button6_Click(object sender, System.EventArgs e)
    {

    Form2 F2 = new Form2();
    sqlConnection1.Open();
    MyAdapt.Fill(MDSet, "MovTable 1");
    F2.dataGrid1.DataSource = MDSet;
    }

    private void button7_Click(object sender, System.EventArgs e)
    {
    this.Close();
    sqlConnection1.Close();
    }

    private void sqlDataAdapter1_RowUpdated(object sender, System.Data.SqlClient.SqlRowUpdatedEventArgs e)
    {

    }
    }
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Please help with SQL Server Express 5

    Can't look at that mess without you going back to add code tags.
    Code:
    ' only way to read it
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2005
    Location
    Vienna, Austria
    Posts
    4,538

    Re: Please help with SQL Server Express 5

    Quote Originally Posted by dglienna
    Can't look at that mess without you going back to add code tags.
    And in the codetags do a correct formatting. The best way is to use the formatted code and paste it into the standard texteditor, then copy it from there to here between the codetags and you will have a wonderful formatting. And sign the line where the exception occurs.
    Jonny Poet

    To be Alive is depending on the willingsness to help others and also to permit others to help you. So lets be alive. !
    Using Code Tags makes the difference: Code is easier to read, so its easier to help. Do it like this: [CODE] Put Your Code here [/code]
    If anyone felt he has got help, show it in rating the post.
    Also dont forget to set a post which is fully answered to 'resolved'. For more details look to FAQ's about Forum Usage. BTW I'm using Framework 3.5 and you ?
    My latest articles :
    Creating a Dockable Panel-Controlmanager Using C#, Part 1 | Part 2 | Part 3 | Part 4 | Part 5 | Part 6 | Part 7

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Please help with SQL Server Express 5

    From what I can see "MyAdapt" is never initialised and gets the default value for its type (i.e. null) and hence the NullReferenceException you're getting. Check it out.

    Hope it helps.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured