Click here to Skip to main content
16,004,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Protected Sub AddNewRecord()
        Dim _connStr As String = SharedLibrary.GetConfigSetting("MRI.ConnectionString")

        Using conn As SqlConnection = New SqlConnection(_connStr)
            Dim sql As String = "Insert into TB_NABERS_SUSTAINABILITY (BldgId,UTILITY_TYPE,FRMDT,TODT,UNIT_USAGE,UNIT_TYPE,GP,WRating) values (@BLDGID,@UTILITY_TYPE,@FRMDT,@TODT,@UNIT_USAGE,@UNIT_TYPE,'0.0',@WRating)"

            Using cmd As SqlCommand = New SqlCommand(sql, conn)

                cmd.Parameters.AddWithValue("@BLDGID", BldgId.Trim())
                cmd.Parameters.AddWithValue("@UTILITY_TYPE", naberText(UtilityType))
                cmd.Parameters.AddWithValue("@FRMDT", DateTime.ParseExact(txtFromDate.Text.Trim(), "d/M/yyyy", CultureInfo.InvariantCulture))
                cmd.Parameters.AddWithValue("@TODT", DateTime.ParseExact(txtToDate.Text.Trim(), "d/M/yyyy", CultureInfo.InvariantCulture))
                cmd.Parameters.AddWithValue("@UNIT_USAGE", txtUsage.Text.Trim())
                cmd.Parameters.AddWithValue("@UNIT_TYPE", naberUnit(UtilityType))
                'If txtGreenPower.Text.Trim() = "" Then
                '    cmd.Parameters.AddWithValue("@GP", 0.0)
                'Else
                '    cmd.Parameters.AddWithValue("@GP", txtGreenPower.Text.Trim())
                'End If

                If TextWaterRating.Text.Trim = "" Then
                    cmd.Parameters.AddWithValue("@WRating", 0.0)
                Else
                    cmd.Parameters.AddWithValue("@WRating", Convert.ToDecimal(TextWaterRating.Text.Trim()))
                End If

                conn.Open()
                cmd.ExecuteNonQuery()

                conn.Close()
            End Using


i have the table TB_NABERS_SUSTAINABILITY column Wrating Decimal(10,2)

What I have tried:

i have the table TB_NABERS_SUSTAINABILITY column Wrating Decimal(10,2)


i have also converted to decimal
Posted
Updated 27-Apr-17 21:39pm

1 solution

Don't just pass user inputs to SQL and let it sort it out: check and convert user inputs to appropriate datatypes before you start with the DB stuff, and report problems direct to the user.
VB
Dim fromDate As DateTime
If Not DateTime.TryParseExact(txtFromDate.Text.Trim(), "d/M/yyyy", CultureInfo.InvariantCulture, fromDate) Then
	... Report problem to user ...
	Return
End If
...
Dim unitUsage As Decimal
If Not Decimal.TryParse(txtUsage.Text.Trim(), uintUsage) Then
	... Report problem to user ...
	Return
End If
...
cmd.Parameters.AddWithValue("@FRMDT", fromDate)
cmd.Parameters.AddWithValue("@TODT", toDate)
cmd.Parameters.AddWithValue("@UNIT_USAGE", unitUsage)
That way, the user gets a better error massage than your app crashing, and you get more robust code that is a lot easier to read.
 
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