Visual Basic.NET - Problema con nulos en vb .net

 
Vista:

Problema con nulos en vb .net

Publicado por Felipe Montero (1 intervención) el 12/02/2009 14:26:21
Hola muchachos y muchachas

Estoy usando la siguiente funcion:

Public Shared Function BuscarPrecioVariedad(ByVal IdCustomer As
String, ByVal Variedad As String, ByVal Cultivo As String) As String
Dim SQL As String
Dim precio As String
Dim ConSQL As SqlClient.SqlConnection = New
SqlClient.SqlConnection()
Dim CommSQL As SqlClient.SqlCommand = New SqlClient.SqlCommand
()

'Busca que existan las tres combinaciones posibles
SQL = "Select price from EXP_PRICES where IdCliente = @Cust
and IdCultivo = @Var and IdVariedad = @planta"
ConSQL.ConnectionString = "Data Source=" + Main.Servidor +
";Initial Catalog=" + Main.GlobalDB + ";Integrated Security=True"
' Abrimos la conexion
ConSQL.Open()
CommSQL.Connection = ConSQL
'Asignamos la consulta que queremos hacer
CommSQL.CommandText = SQL
'Creamos y asignamos valor a los parametros
CommSQL.Parameters.Add("@Cust", SqlDbType.SmallInt)
CommSQL.Parameters("@Cust").Value = IdCustomer
CommSQL.Parameters.Add("@planta", SqlDbType.SmallInt)
CommSQL.Parameters("@planta").Value = Variedad
CommSQL.Parameters.Add("@Var", SqlDbType.SmallInt)
CommSQL.Parameters("@Var").Value = Cultivo
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
'Busca la combinacion de Cliente y Variedad
SQL = "Select price from EXP_PRICES where IdCliente =
@Cust and IdCultivo = @Var"
CommSQL.CommandText = SQL
CommSQL.Parameters.Add("@Cust", SqlDbType.SmallInt)
CommSQL.Parameters("@Cust").Value = IdCustomer
CommSQL.Parameters.Add("@Var", SqlDbType.SmallInt)
CommSQL.Parameters("@Var").Value = Cultivo
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
' Aqui busca la combinacion de cliente y planta
SQL = "Select price from EXP_PRICES where IdCliente =
@Cust and IdVariedad = @planta"
CommSQL.CommandText = SQL
CommSQL.Parameters.Add("@Cust", SqlDbType.SmallInt)
CommSQL.Parameters("@Cust").Value = IdCustomer
CommSQL.Parameters.Add("@planta", SqlDbType.SmallInt)
CommSQL.Parameters("@planta").Value = Variedad
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
' Busca el precio para cliente
SQL = "Select price from EXP_PRICES where
IdCliente = @Cust"
CommSQL.CommandText = SQL
CommSQL.Parameters.Add("@Cust",
SqlDbType.SmallInt)
CommSQL.Parameters("@Cust").Value = IdCustomer
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
' Buscar el precio por variedad
SQL = "Select price from EXP_PRICES where
IdCultivo = @Var"
CommSQL.CommandText = SQL
CommSQL.Parameters.Add("@Var",
SqlDbType.SmallInt)
CommSQL.Parameters("@Var").Value = Cultivo
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
' Busca el precio por planta
SQL = "Select price from EXP_PRICES where
IdVariedad = @planta"
CommSQL.Parameters.Add("@planta",
SqlDbType.SmallInt)
CommSQL.Parameters("@planta").Value =
Variedad
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
Return "0"
End If 'fin de la busqueda por planta
End If ' fin de consulta por variedad
End If 'fin de la consulta de cliente
End If 'fin del if de cliente y planta
End If 'fin del if de Cliente y variedad
End If 'fin del if de la combinacion de las tres variedades
'Cerramos la conexion
ConSQL.Close()
End Function

Como veran, voy haciendo comparaciones

If precio.Length > 0 Then
Return precio
Else
' Aqui busca la combinacion de cliente y planta
SQL = "Select price from EXP_PRICES where IdCliente =
@Cust and IdVariedad = @planta"
CommSQL.CommandText = SQL
CommSQL.Parameters.Add("@Cust", SqlDbType.SmallInt)
CommSQL.Parameters("@Cust").Value = IdCustomer
CommSQL.Parameters.Add("@planta", SqlDbType.SmallInt)
CommSQL.Parameters("@planta").Value = Variedad
precio = CommSQL.ExecuteScalar.ToString()
If precio.Length > 0 Then
Return precio
Else
end if
End if

La idea, es que si no devolvio nada, el primer if, pase al segundo y
si ese tambien, devolvio nada, que pase al siguiente:

Pero si llega al primero y no habia nada, hasta ahi llega...

Como puedo solucionar ese problema, es que la verdad soy bastante
nuevo, en .net y todavia no lo manejo bien.

Gracias de antemano por su ayuda.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder

RE:Problema con nulos en vb .net

Publicado por like (20 intervenciones) el 12/02/2009 21:07:56
intenta comparando con

if precio <> dbnull.value then
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar