Where is the mistake in my source?

Jun 24 2009 7:24 AM
 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlConnection conn;
        SqlDataAdapter da;
        DataSet ds;
        SqlCommandBuilder cmb;

        public Form1()
        {
            InitializeComponent();

            SqlConnection conn = new SqlConnection("Data Source=MACHINE;Initial Catalog=JobDetailsDB;Integrated Security=True");
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select * FROM JobDetailsDB", conn);
            DataSet ds = new DataSet();
            SqlCommandBuilder cmb = new SqlCommandBuilder(da);
            
            dg.DataSource = ds.Tables["JobDetailsTbl"];

           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            

        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            da.UpdateCommand = cmb.GetUpdateCommand();

            da.Update(ds);
        }
      
    }

}

This is the code.When I try to compile it I`m getting this:

Warning    1    The field 'WindowsFormsApplication1.Form1.conn' is never used    C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    18    23    WindowsFormsApplication1


Warning    2    Field 'WindowsFormsApplication1.Form1.da' is never assigned to, and will always have its default value null    C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    19    24    WindowsFormsApplication1


Warning    3    Field 'WindowsFormsApplication1.Form1.ds' is never assigned to, and will always have its default value null    C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    20    17    WindowsFormsApplication1


Warning    4    Field 'WindowsFormsApplication1.Form1.cmb' is never assigned to, and will always have its default value null    C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs    21    27    WindowsFormsApplication1


and this:


System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="WindowsFormsApplication1"
  StackTrace:
       at WindowsFormsApplication1.Form1.btnUpdate_Click(Object sender, EventArgs e) in C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 47
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(Form mainForm)
       at WindowsFormsApplication1.Program.Main() in C:\Documents and Settings\ICHO\My Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException:
for this line: 
 da.UpdateCommand = cmb.GetUpdateCommand();

The program runs after I compile it but when I try to update the table it is not responding.
Can someone help me please I`m new in C# and I cant manage with this problem.
P.S Sorry if my English is bad.



Answers (1)