Click here to Skip to main content
11,578,524 members (60,188 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
 public void FillDT()
        {
            OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if (sb == null||sb.DataSource!=null||sfd.FileName!=null)
            {
                sb.DataSource =sfd.FileName;
                return;
            }
           
            bookConn = new OleDbConnection(sb.DataSource=sfd.FileName);
            bookConn.Open();
            oleDbCmd.Connection = bookConn;
            
            oleDbCmd.CommandText = "INSERT INTO students (Firstname,Lastname,Age,Gender,School,Gfirstname,Glastname,Contact,Occupation,PostalAddr) Values('" + this.FirstnameTB.Text + "','" + this.LastnameTB.Text + "','" + this.AgeTB.Text + "','" + this.GenderTB.Text + "','" + this.SchoolTB.Text + "','" + this.GFirstnameTB.Text + "','" + this.GLastnameTB.Text + "','" + this.ContactTB.Text + "','" + this.OccupationTB.Text + "','" + this.AddressTB.Text + "');";
            int temp = oleDbCmd.ExecuteNonQuery();
 
            if (temp > 0)
            {
                FirstnameTB.Text = null;
                LastnameTB.Text = null;
                AgeTB.Text = null;
                GenderTB.Text = null;
                SchoolTB.Text = null;
                GFirstnameTB.Text = null;
                GLastnameTB.Text = null;
                ContactTB.Text = null;
                OccupationTB.Text = null;
                AddressTB.Text = null;
                MessageBox.Show("Record Successfuly Added", "Entry Added");
            }
            else
            {
                MessageBox.Show("Failed to Add Record", "Entry failure");
            }
            bookConn.Close();
        }




The exception occurs at "sb.DataSource= sfd.FileName;"
Posted 8-Jan-13 0:46am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Ehem :cough:
            OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if (sb == null||sb.DataSource!=null||sfd.FileName!=null)
            {
                sb.DataSource =sfd.FileName;
                return;
            }
sb is null - you set it to it, so the test will always pass on the first test (the others will not be done)
Perhaps you meant
            if (sb != null||sb.DataSource!=null||sfd.FileName!=null)
But in that case, it will always fail...
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

try to add check your condition
OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if (sb == null||sb.DataSource!=null||sfd.FileName!=null)
            {
                sb.DataSource =sfd.FileName;
                return;
            }
In above code your sb is always Null so that if "sfd.FileName == Null or Not Null" does meant. due to || (Or Condition in if ). so try to chenge your condition like below
OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if ((sb == null||sb.DataSource!=null) && (sfd.FileName!=null))
            {
                sb.DataSource =sfd.FileName;
                return;
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You have not allocated memory for the OleDbConnectionStringBuilder variable sb

 OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if (sb == null||sb.DataSource!=null||sfd.FileName!=null)
            {
                sb = new OleDbConnectionStringBuilder(); 
                sb.DataSource =sfd.FileName;
                return;
            }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

use of try catch statement. It also remind the user to fill the required place or leave the catch block, if you don't want to show any message.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100