Visual Basic.NET - LLENAR COMBOX CON PROCEDIMIENTO ALMACENADO

 
Vista:

LLENAR COMBOX CON PROCEDIMIENTO ALMACENADO

Publicado por HECTOR (55 intervenciones) el 22/12/2015 19:38:49
ALGUIEN SABE COMO PUEDO LLENAR COMBO BOX CON UN PROCEDIMIENTO ALMACENADO.
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
Imágen de perfil de Luis

LLENAR COMBOX CON PROCEDIMIENTO ALMACENADO

Publicado por Luis (11 intervenciones) el 23/12/2015 16:36:29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
CREATE PROCEDURE [dbo].[pestado]
 
@accion    int=null,
@idestado  int=null,
@nombreestado    nvarchar(50) = null
AS
SET NOCOUNT ON
 
if @accion = 1
begin
 
select nombreestado from testado order by nombreestado
end
 
 
Public Class DaoEstados
 
    Dim db As New Conexion.Database
 
Public Function ObtenerListaEstadosforCommbo() As Object
        Try
 
            Dim arrParams As New ArrayList
            arrParams.Add(New System.Data.SqlClient.SqlParameter("@accion", 1))
 
            Return db.ExecuteSP("pestado", arrParams)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
End Class
 
Dim daoc as new daoestados
 
        ComboEstado.DataSource = daoc.ObtenerListaEstadosforCommbo
        ComboEstado.DisplayMember = "NombreEstado"
        ComboEstado.SelectedIndex = -1


Espero que esto te oriente
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

LLENAR COMBOX CON PROCEDIMIENTO ALMACENADO

Publicado por hector (55 intervenciones) el 23/12/2015 18:31:54
Me sale un error con la conexión.
Me puedes orientar sobre la clase de conexión que le haces
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
Imágen de perfil de Luis

LLENAR COMBOX CON PROCEDIMIENTO ALMACENADO

Publicado por Luis (11 intervenciones) el 23/12/2015 23:32:36
Te paso el codigo de conexion que manejo, espero te sirva
Es el codigo completo
considero que con esto es mas que suficiente para hacer lo que ocupas



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
Imports System.Data
Imports System.Data.SqlClient
Public Class Database
 
    Dim connStr As String = "Data Source=TUPC\SQLEXPRESS;Initial Catalog=tubd;Integrated Security=True"
    Dim conn As New SqlConnection
 
    Public Sub New()
        conn.ConnectionString = connStr
    End Sub
 
    Public Sub OpenConnection()
        Try
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
    Public Sub CloseConnection()
        Try
            If conn.State <> ConnectionState.Closed Then
                conn.Close()
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
    Public Function ExecuteSP(ByVal spName As String, ByVal params As ArrayList) As DataTable
        Try
            'crear comando
            Dim command As New SqlCommand
            command.CommandType = CommandType.StoredProcedure
            command.CommandText = spName
            command.CommandTimeout = 5
            command.Connection = conn
 
            For Each param As SqlParameter In params
                command.Parameters.Add(param)
            Next
 
            'metodo de ejecución
            Dim ds As New DataSet 'objeto creado vacio
            Dim da As New SqlDataAdapter(command)
 
            da.Fill(ds)
 
            'asegurarnos de que se obtuvo una respuesta de la BD
            If ds.Tables.Count > 0 Then
                Return ds.Tables(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Function ExecuteQuery(ByVal sql As String) As DataTable
        Try
            Dim command As New SqlCommand
            command.CommandType = CommandType.Text
            command.CommandText = sql
            command.CommandTimeout = 5
            command.Connection = conn
 
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(command)
            da.Fill(ds)
 
            If ds.Tables.Count > 0 Then
                Return ds.Tables(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Function
 
    Public Sub ExecuteNonQuery(ByVal sql As String)
        Try
            Dim command As New SqlCommand
            command.CommandType = CommandType.Text
            command.CommandText = sql
            command.CommandTimeout = 5
            command.Connection = conn
 
            command.ExecuteNonQuery()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
 
    Public Function ExecuteSPWithTransaction(ByVal spName As String, ByVal params As ArrayList) As DataTable
        Dim transaction As SqlTransaction = conn.BeginTransaction(IsolationLevel.Snapshot)
        Try
            Dim command As New SqlCommand
            command.CommandType = CommandType.StoredProcedure
            command.CommandText = spName
            command.CommandTimeout = 5
            command.Connection = conn
            For Each param As SqlParameter In params
                command.Parameters.Add(param)
            Next
 
            command.Transaction = transaction
 
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter
            da.Fill(ds)
            transaction.Commit()
 
            If ds.Tables.Count > 0 Then
                Return ds.Tables(0)
            Else
                Return Nothing
            End If
        Catch ex As Exception
            transaction.Rollback()
            Throw ex
        End Try
    End Function
 
    Public Function ExecuteSPWithOutParam(ByVal spName As String, ByVal params As ArrayList, ByVal indexParamSalida As Int16) As Object
        Try
            Dim command As New SqlCommand
            command.CommandType = CommandType.StoredProcedure
            command.CommandText = spName
            command.CommandTimeout = 5
            command.Connection = conn
            For Each param As SqlParameter In params
                command.Parameters.Add(param)
            Next
 
            Dim ds As New DataSet
            Dim da As New SqlDataAdapter(command)
            da.Fill(ds)
 
            Dim obj As Object = command.Parameters.Item(indexParamSalida).Value
 
            Return obj
        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class
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