Click here to Skip to main content
15,884,237 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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

Ehem :cough:
C#
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
C#
if (sb != null||sb.DataSource!=null||sfd.FileName!=null)
But in that case, it will always fail...
 
Share this answer
 
try to add check your condition
C#
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
C#
OleDbConnectionStringBuilder sb=null;
            SaveFileDialog sfd = new SaveFileDialog();
            if ((sb == null||sb.DataSource!=null) && (sfd.FileName!=null))
            {
                sb.DataSource =sfd.FileName;
                return;
            }
 
Share this answer
 
You have not allocated memory for the OleDbConnectionStringBuilder variable sb

C#
OleDbConnectionStringBuilder sb=null;
           SaveFileDialog sfd = new SaveFileDialog();
           if (sb == null||sb.DataSource!=null||sfd.FileName!=null)
           {
               sb = new OleDbConnectionStringBuilder();
               sb.DataSource =sfd.FileName;
               return;
           }
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900