Click here to Skip to main content
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 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  
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  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 525
1 CPallini 505
2 George Jonsson 353
3 OriginalGriff 220
4 Pheonyx 218
0 OriginalGriff 4,813
1 CPallini 3,890
2 Sergey Alexandrovich Kryukov 3,274
3 George Jonsson 2,552
4 Gihan Liyanage 2,186


Advertise | Privacy | Mobile
Web01 | 2.8.140905.1 | Last Updated 8 Jan 2013
Copyright © CodeProject, 1999-2014
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