none
input string was not in a correct format RRS feed

  • Question

  • I am facing a problem when I am trying to insert data to my database.I have checked my database datatype and the c# datatype.Everything is ok.But still can't understand the problem where is the problem. Here is the problem code
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using Co_Operative_Scoiety_Software.BLL;
    using Co_Operative_Scoiety_Software.DAL.DAO;
    using Co_Operative_Scoiety_Software.Utility;
    using System.IO;
    
    
    namespace Co_Operative_Scoiety_Software
    {
        public partial class openNewCreditAccount : Co_Operative_Scoiety_Software.Home
        {
            CreditAccountManager _creditAccountManager = new CreditAccountManager();
            Image photoimg, signatureimg;
            public openNewCreditAccount()
            {
                InitializeComponent();
    
                religionComboBox.Items.Add("Christian");
                genderComboBox.Items.Add("Male");
                genderComboBox.Items.Add("Female");
                genderComboBox.Items.Add("Others");
                accountStatusComboBox.Items.Add("Active");
                accountStatusComboBox.Items.Add("Inactive");
                accountStatusComboBox.Items.Add("Closed");
                accountStatusComboBox.Items.Add("Dead");
                depositTypeComboBox.Items.Add("Daily");
                depositTypeComboBox.Items.Add("Weekly");
                depositTypeComboBox.Items.Add("Monthly");
                depositTypeComboBox.Items.Add("Yearly");
            }
    
            private void signatureFileButton_Click(object sender, EventArgs e)
            {
                signatureOpenFileDialog.Title = "Insert Image";
                signatureOpenFileDialog.DefaultExt = ".";
                signatureOpenFileDialog.Filter = "All Files|*.*|JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png";
                signatureOpenFileDialog.FilterIndex = 1;
    
                DialogResult result = signatureOpenFileDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    signatureLabel.Text = " ";
    
                    try
                    {
    
                        string theImagePath1 = signatureOpenFileDialog.FileName;
    
                        //Image img;
    
                        signatureimg = Image.FromFile(theImagePath1);
    
                        Clipboard.SetDataObject(signatureimg);
    
                        DataFormats.Format df;
    
                        df = DataFormats.GetFormat(DataFormats.Bitmap);
    
                        if (this.showSignatureRichTextBox.CanPaste(df))
                        {
    
                            this.showSignatureRichTextBox.Paste(df);
    
                        }
    
                    }
    
                    catch
                    {
    
                        MessageBox.Show("Unable to insert image.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                    }
                }
            }
    
            private void backButton_Click(object sender, EventArgs e)
            {
                Home home = new Home();
                this.Hide();
                home.ShowDialog();
            }
    
            private void cancelButton_Click(object sender, EventArgs e)
            {
                Home home = new Home();
                this.Hide();
                home.ShowDialog();
            }
    
            private void photoButton_Click(object sender, EventArgs e)
            {
                photoOpenFileDialog.Title = "Insert Image";
                photoOpenFileDialog.DefaultExt = "bmp";
                photoOpenFileDialog.Filter = "All Files|*.*|JPEG Files|*.jpg|GIF Files|*.gif|PNG Files|*.png";
                photoOpenFileDialog.FilterIndex = 1;
    
                DialogResult result = photoOpenFileDialog.ShowDialog();
                if (result == DialogResult.OK)
                {
                    photoLabel.Text = " ";
                    
                    try
                    {
    
                        string theImagePath1 = photoOpenFileDialog.FileName;
    
                        //Image img;
    
                        photoimg = Image.FromFile(theImagePath1);
    
                        Clipboard.SetDataObject(photoimg);
    
                        DataFormats.Format df;
    
                        df = DataFormats.GetFormat(DataFormats.Bitmap);
    
                        if (this.showImageRichTextBox.CanPaste(df))
                        {
    
                            this.showImageRichTextBox.Paste(df);
    
                        }
    
                    }
    
                    catch
                    {
    
                        MessageBox.Show("Unable to insert image.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    
                    }
                }
                
            }
    
            protected void saveButton_Click(object sender, EventArgs e)
            {
                try
                {
                    var creditAccount = new CreditAccountModel
                    {
                        CreditID = Convert.ToInt32(creditIDTextBox.Text),
                        FullName = Convert.ToString(creditNameTextBox.Text),
                        FatherName = Convert.ToString(fatherNameTextBox.Text),
                        MotherName = Convert.ToString(motherNameTextBox.Text),
                        PresentAddress = Convert.ToString(presentAddressRichTextBox.Text),
                        PermanentAddress = Convert.ToString(permanentAddressRichTextBox.Text),
                        VoterID = Convert.ToString(voterIDTextBox.Text),
                        Phone = Convert.ToString(phoneTextBox.Text),
                        Mobile = Convert.ToString(mobileTextBox.Text),
                        //DOB = Convert.ToDateTime(dateOfBirthDateTimePicker.Text),
                        DOB = DateTime.ParseExact(dateOfBirthDateTimePicker.Text, "dd/MM/yyyy", null),
                        Occupation = Convert.ToString(occupationTextBox.Text),
                        Nationality = Convert.ToString(nationalityTextBox.Text),
                        Religion = Convert.ToString(religionComboBox.SelectedValue),
                        Gender = Convert.ToString(genderComboBox.SelectedValue),
                        Email = Convert.ToString(eMailTextBox.Text),
    
                        Photo = photoimg,
                        Signature = signatureimg,
    
                        NomineeName1 = Convert.ToString(nomineeName1TextBox.Text),
                        NomineeFatherName1 = Convert.ToString(nomineeFatherName1TextBox.Text),
                        NomineeAge1 = Convert.ToInt32(nomineeAge1TextBox.Text),
                        NomineeRelation1 = Convert.ToString(nomineeRelation1TextBox.Text),
                        NomineeAddress1 = Convert.ToString(nomineeAddress1TextBox.Text),
                        NomineeContactNo1 = Convert.ToString(nomineeContact1TextBox.Text),
                        NomineeShare1 = Convert.ToDecimal(nomineeShare1TextBox.Text),
                        NomineeComment1 = Convert.ToString(nomineeComment1TextBox.Text),
                        NomineeName2 = Convert.ToString(nomineeName2TextBox.Text),
                        NomineeFatherName2 = Convert.ToString(nomineeFatherName2TextBox.Text),
                        NomineeAge2 = Convert.ToInt32(nomineeAge2TextBox.Text),
                        NomineeRelation2 = Convert.ToString(nomineeRelation2TextBox.Text),
                        NomineeAddress2 = Convert.ToString(nomineeAddress2TextBox.Text),
                        NomineeContactNo2 = Convert.ToString(nomineeContact2TextBox.Text),
                        NomineeShare2 = Convert.ToDecimal(nomineeShare2TextBox.Text),
                        NomineeComment2 = Convert.ToString(nomineeComment2TextBox.Text),
    
                        IntroducerCreditID = Convert.ToInt32(introducerCreditIDTextBox.Text),
                        IntroducerName = Convert.ToString(introducerCreditNameTextBox.Text),
                        IntroducerCreditAccountNo = Convert.ToString(introducerCreditAccountNoTextBox.Text),
                        CreditAccountNo = Convert.ToString(accountNumberTextBox.Text),
                        OpeningBalance = Convert.ToDecimal(openingBalanceTextBox.Text),
                        //StartingDate = Convert.ToDateTime(startingDateDateTimePicker.Text),
                        StartingDate = DateTime.ParseExact(startingDateDateTimePicker.Text, "dd/MM/yyyy", null),
                        InterestRate = Convert.ToDecimal(interestRateTextBox.Text),
                        AccountStatus = Convert.ToString(accountStatusComboBox.SelectedValue),
                        DepositType = Convert.ToString(depositTypeComboBox.SelectedValue)
    
                    };
    
                    if (_creditAccountManager.AddCreditAccount(creditAccount))
                    {
                        messageLabel.Text = "Added Successfully";
                        
    
                    }
                    else { messageLabel.Text = "There some error"; }
                }
                catch (Exception exception)
                {
                    messageLabel.Text = exception.Message;
                }
            }
    
            private void introducerCheckBox_CheckedChanged(object sender, EventArgs e)
            {
                if(introducerCheckBox.Checked == true)
                {
                    introducerCreditIDTextBox.Enabled = false;
                    introducerCreditNameTextBox.Enabled = false;
                    introducerCreditAccountNoTextBox.Enabled = false;
                }
            }
    
           
        }
    }


    • Edited by arov90 Sunday, May 24, 2015 12:42 PM
    Sunday, May 24, 2015 12:36 PM

Answers

  • I don't see any code to persist the image but this is what you should be doing.

    To save an image to Sql Server.

    So when you read in your image from your dialog box you should read in all the bytes to a byte array so you can assign it to whatever value is eventually assigned to the SqlParameter.


    Mark as answer or vote as helpful if you find it useful | Igor

    Monday, May 25, 2015 12:56 PM

All replies

  • It would help if you tell us which line of code gets the error? I'm going to guess that it's

    DOB = DateTime.ParseExact(dateOfBirthDateTimePicker.Text, "dd/MM/yyyy", null),

    and that he problem is that the text in the DateTimePicker is not in the format you expect. Assuming that DOB is a DateTime value, you don't need to convert String to DateTime, you can just use the Value property of the DateTimePicker.

    DOB = dateOfBirthDateTimePicker.Value,

    Sunday, May 24, 2015 12:49 PM
  • I insert input data and click the save button it shows me this  from "catch" block.I can't figure out which line has the problem from "try" block.I use your  DOB code instead of mine. But still has the same problem .

    • Edited by arov90 Sunday, May 24, 2015 1:59 PM
    Sunday, May 24, 2015 1:57 PM
  • I insert input data and click the save button it shows me this  from "catch" block.I can't figure out which line has the problem from "try" block.I use your  DOB code instead of mine. But still has the same problem .


    Then get rid of the Try/Catch so that you can find out where the problem is.
    Sunday, May 24, 2015 3:03 PM
  • I insert input data and click the save button it shows me this  from "catch" block.I can't figure out which line has the problem from "try" block.I use your  DOB code instead of mine. But still has the same problem .

    Change your catch block like this.

    catch (Exception exception)
    {
       messageLabel.Text = string.Format("Exception type: '{0}'. Message: '{1}'. Stack trace: '{2}'", exception.GetType().FullName, exception.Message, exception.StackTrace);
    }

    This will now give you the exception type which I assume is a System.FormatException but we will see. It will also print the stacktrace which will include the call that fails and the line number if there are pdb files in the build.

    Once you figure out what the call is and on what line it should then be more clear what value is causing the problem. If not please reply with the details from the catch block as I printed above.


    Mark as answer or vote as helpful if you find it useful | Igor

    Sunday, May 24, 2015 3:23 PM
  • Thank you again :-) 

    I think that the Problem is occurred when the I insert the image file to database</g></g></g></g>.My datatype is Image in sql</g>. Is my code OK for save image file? Can you give me a solution about it</g> please ?</g>

    Photo = photoimg,
    Signature = signatureimg,








    • Edited by arov90 Sunday, May 24, 2015 10:01 PM
    Sunday, May 24, 2015 9:52 PM
  • Thank you again :-) 

    I think that the Problem is occurred when the I insert the image file to database</g></g></g></g>.My datatype is Image in sql</g>. Is my code OK for save image file? Can you give me a solution about it</g> please ?</g>

    Photo = photoimg,
    Signature = signatureimg,




    • Edited by arov90 Sunday, May 24, 2015 10:00 PM
    Sunday, May 24, 2015 9:55 PM

  • I think that the Problem <g class="gr_ gr_9 gr-alert gr_gramm gr_hide Grammar" data-gr-id="9" id="9">is occurred when the I insert the image file to database</g></g></g></g>.My datatype is Image in sql</g>. Is my code OK for save image file? Can you give me a solution about it</g> please ?</g>

    Photo = photoimg,
    Signature = signatureimg,




    Your post is garbled and I can't be sure what you are saying. Please post a few lines of code and describe what you expect them to do and what actually happens.
    Sunday, May 24, 2015 10:03 PM
  • Thank you again :-) 

    I think that the Problem is occurred when the I insert the image file to database</g></g></g></g>.My datatype is Image in sql</g>. Is my code OK for save image file? Can you give me a solution about it</g> please ?</g>

    Photo = photoimg,
    Signature = signatureimg,




    Your response is not clear.

    Please change your catch handler in your image as well to what I have outlined above, I will provide it here one more time. An empty catch is bad coding because you will never know that something bad happened, always log your exceptions at the very least or do not provide a try/catch at all.

    catch (Exception exception)
    {
       messageLabel.Text = string.Format("Image handling exception. Type: '{0}'. Message: '{1}'. Stack trace: '{2}'", exception.GetType().FullName, exception.Message, exception.StackTrace);
    }
    Again, please provide us with the complete exception detail so we can better understand why you are getting an error.



    Mark as answer or vote as helpful if you find it useful | Igor

    Monday, May 25, 2015 12:12 AM
  • Exception type: 'System.ArgumentException'. Message: 'No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type.'

    This Exeception</g> I got. It might be the problem is saving image</g> in database</g>.  My code for this to save image file in database</g>,

    Photo = photoimg,
    Signature = signatureimg,

    Thank you guys one more time for your kindness.




    • Edited by arov90 Monday, May 25, 2015 6:17 AM
    Monday, May 25, 2015 6:14 AM
  • Exception type: 'System.ArgumentException'. Message: 'No mapping exists from object type System.Drawing.Bitmap to a known managed provider native type.'

    This Exeception</g> I got. It might be the problem is saving image</g> in database</g>.  My code for this to save image file in database</g>,

    Photo = photoimg,
    Signature = signatureimg,

    Thank you guys one more time for your kindness.



    • Edited by arov90 Monday, May 25, 2015 6:18 AM
    Monday, May 25, 2015 6:17 AM
  • I don't see any code to persist the image but this is what you should be doing.

    To save an image to Sql Server.

    So when you read in your image from your dialog box you should read in all the bytes to a byte array so you can assign it to whatever value is eventually assigned to the SqlParameter.


    Mark as answer or vote as helpful if you find it useful | Igor

    Monday, May 25, 2015 12:56 PM
  • Thank you mate finally it runs .</g> Sometimes we just forget and just hurry to solve without acknowledging :-) 


    • Edited by arov90 Monday, May 25, 2015 8:24 PM
    Monday, May 25, 2015 8:23 PM