hi there,

i have a mask textbox in C# which saves $9999999999.99 as the mask, i have a database field should take the value and save it. the database feield is in money .but when i am writing the C# method to insert it into the data i convert the string into double and then i am trying to insert it into a money database field.

but when the mask text box value is empty it gives an error FormatException saying that the input string was not formatted correctly
tha code is below the error comes from the place where i have bolede the code

public void AddTopicDetails(int phase, String title, String topicNo, String subTitle, String agency, String type, String status,
            String sessionName,String Amt, DateTime startDate, DateTime endDate, String doNotProceed, String reason, String Option, String OAmt, DateTime Osdate, DateTime Oedate)
        {
            try
            {
                db.openConnection();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Datebase open connection Failed");
            }
            finally
            {
                String query = @"Insert into Topic (Phase,TTitle,TopicNo,SubTitle,Agency,Type,Status,TSessionName,TAmount,TSDate,TEDate,DoNotProceed,Reason,TOption,TOptionAmt,TOptionSDate,TOptionEDate) 
                        Values (@phase,@title,@topicNo,@subTitle,@agency,@type,@status,@sessionName,@tamt,@sDate,@eDate,@doNotProceed,@reason,@option,@oamt,@osdate,@oedate)";

                SqlCommand command = new SqlCommand(query,DB.getConnection());
                
                command.Parameters.Add("@phase", SqlDbType.VarChar).Value = phase;
                command.Parameters.Add("@title", SqlDbType.VarChar).Value = title;
                command.Parameters.Add("@topicNo", SqlDbType.VarChar).Value = topicNo;
                command.Parameters.Add("@subTitle", SqlDbType.VarChar).Value = subTitle;
                command.Parameters.Add("@agency", SqlDbType.VarChar).Value = agency;
                command.Parameters.Add("@type", SqlDbType.VarChar).Value = type;
                command.Parameters.Add("@status", SqlDbType.VarChar).Value = status;
                command.Parameters.Add("@sessionName", SqlDbType.VarChar).Value = sessionName;
                [B]command.Parameters.Add("@tamt", SqlDbType.Money).Value = Double.Parse(Amt);[/B]
                command.Parameters.Add("@sDate", SqlDbType.DateTime).Value = startDate;
                command.Parameters.Add("@eDate", SqlDbType.DateTime).Value = endDate;
                command.Parameters.Add("@doNotProceed", SqlDbType.VarChar).Value = doNotProceed;
                command.Parameters.Add("@reason", SqlDbType.VarChar).Value = reason;
                command.Parameters.Add("@option", SqlDbType.VarChar).Value =  Option;
                [B]command.Parameters.Add("@oamt", SqlDbType.Money).Value = Double.Parse(OAmt);[/B]
                command.Parameters.Add("@osdate", SqlDbType.DateTime).Value = Osdate;
                command.Parameters.Add("@oedate", SqlDbType.DateTime).Value =  Oedate;

                command.ExecuteNonQuery();
                db.closeConnection();
            }
        }

what is the error in my code. w@ should i do to avoid this

thanxxxxxxxxxxxx

You should remove the "$" sign from the text. And also, it is recommended to use Decimal data type for Money.

To remove the "$" you can use Replace() method:

command.Parameters.Add("@tamt", SqlDbType.Money).Value = [B]Decimal[/B].Parse(Amt.Replace("$", string.Empty);

command.Parameters.Add("@oamt", SqlDbType.Money).Value = [B]Decimal[/B].Parse(OAmt.Replace("$", string.Empty);

Thanks

You should remove the "$" sign from the text. And also, it is recommended to use Decimal data type for Money.

To remove the "$" you can use Replace() method:

command.Parameters.Add("@tamt", SqlDbType.Money).Value = [B]Decimal[/B].Parse(Amt.Replace("$", string.Empty);

command.Parameters.Add("@oamt", SqlDbType.Money).Value = [B]Decimal[/B].Parse(OAmt.Replace("$", string.Empty);

Thanks

hey,
but when the value is empty how can i hadle it . when the value is empty it gives an Exception saying Format Exception was unhandled, input string was not formatted correctly. how can i handle this situation

thanxxxx.

This article has been dead for over six months. Start a new discussion instead.