
poner datos en un textbox desde tabla sql server
Publicado por andyman (2 intervenciones) el 31/10/2012 20:54:50
buenas tardes
esta es mi primera pregunta en el foro y quisiera saber si me pueden ayudar con esto tengo una pagina con dos campos uno para codigo y otro para nombre, puedo insertar, modificar y eliminar los datos de la tabla en sql server pero no he podido traer esos datos hacia el textbox nombre el codigo que tengo es este, es un poco enredado pero me ha funcionado.
Imports System.Data.SqlClient
Imports System.Data
Public Class _Default
Inherits System.Web.UI.Page
Dim obj As New Raza
Function cnx_insert2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim insertar2 As String
'insertar2 = "insert into vete_raza (raz_codigo,raz_nombre) values (" & text_codigo.Text & "," ' & text_raza.Text & ')""
insertar2 = "insert into vete_raza (raz_codigo,raz_nombre) values (" & text_codigo.Text & ",'" & text_raza.Text & "')"
'insertar2 = "insert into vete_raza (raz_codigo) values (" & text_codigo.Text & ",'" & text_raza.Text & "')"
Dim cmd_insertar2 As SqlCommand = New SqlCommand(insertar2, con)
Try
con.Open()
cmd_insertar2.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_update2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim actualizar As String
actualizar = "update vete_raza set raz_nombre = '" & text_raza.Text & "' where raz_codigo =" & text_codigo.Text & ""
Dim cmd_actualizar As SqlCommand = New SqlCommand(actualizar, con)
Try
con.Open()
cmd_actualizar.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_delete2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim borrar As String
borrar = "delete vete_raza where raz_codigo =" & text_codigo.Text & ""
Dim cmd_borrar As SqlCommand = New SqlCommand(borrar, con)
Try
con.Open()
cmd_borrar.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_select2() As String
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim consultar As String
consultar = "select (raz_nombre) from vete_raza where raz_codigo =" & text_codigo.Text & ""
Dim cmd_consultar As New SqlCommand(consultar, con)
Try
con.Open()
cmd_consultar.ExecuteNonQuery()
Dim consu As SqlDataReader = cmd_consultar.ExecuteReader
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return Nothing
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub bt_guardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_guardar.Click
Dim resultado As Integer
resultado = cnx_insert2()
MsgBox("los datos se ingresaron correctamente")
End Sub
Protected Sub bt_actualizar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_actualizar.Click
Dim resultado As Integer
resultado = cnx_update2()
MsgBox("el registro se actualizo de forma correcta")
End Sub
Protected Sub bt_borrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_borrar.Click
Dim resultado As Integer
resultado = cnx_delete2()
MsgBox("el registro se elimino")
End Sub
Protected Sub bt_consultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_consultar.Click
Dim resultado As String
resultado = cnx_select2()
text_raza.Text = cnx_select2()
End Sub
End Class
en donde tengo el problema es exactamente en la funcion cnx_select2
si me podrian ayudar se los agradeceria
esta es mi primera pregunta en el foro y quisiera saber si me pueden ayudar con esto tengo una pagina con dos campos uno para codigo y otro para nombre, puedo insertar, modificar y eliminar los datos de la tabla en sql server pero no he podido traer esos datos hacia el textbox nombre el codigo que tengo es este, es un poco enredado pero me ha funcionado.
Imports System.Data.SqlClient
Imports System.Data
Public Class _Default
Inherits System.Web.UI.Page
Dim obj As New Raza
Function cnx_insert2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim insertar2 As String
'insertar2 = "insert into vete_raza (raz_codigo,raz_nombre) values (" & text_codigo.Text & "," ' & text_raza.Text & ')""
insertar2 = "insert into vete_raza (raz_codigo,raz_nombre) values (" & text_codigo.Text & ",'" & text_raza.Text & "')"
'insertar2 = "insert into vete_raza (raz_codigo) values (" & text_codigo.Text & ",'" & text_raza.Text & "')"
Dim cmd_insertar2 As SqlCommand = New SqlCommand(insertar2, con)
Try
con.Open()
cmd_insertar2.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_update2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim actualizar As String
actualizar = "update vete_raza set raz_nombre = '" & text_raza.Text & "' where raz_codigo =" & text_codigo.Text & ""
Dim cmd_actualizar As SqlCommand = New SqlCommand(actualizar, con)
Try
con.Open()
cmd_actualizar.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_delete2() As Integer
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim borrar As String
borrar = "delete vete_raza where raz_codigo =" & text_codigo.Text & ""
Dim cmd_borrar As SqlCommand = New SqlCommand(borrar, con)
Try
con.Open()
cmd_borrar.ExecuteNonQuery()
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return 0
End Function
'''
'''
Function cnx_select2() As String
Dim conexion As String
conexion = "Data Source = ANDYMAN-PC\SQLEXPRESS; initial catalog = veterinaria; integrated security =true"
Dim con As New SqlConnection(conexion)
Dim consultar As String
consultar = "select (raz_nombre) from vete_raza where raz_codigo =" & text_codigo.Text & ""
Dim cmd_consultar As New SqlCommand(consultar, con)
Try
con.Open()
cmd_consultar.ExecuteNonQuery()
Dim consu As SqlDataReader = cmd_consultar.ExecuteReader
con.Close()
Catch e As SqlException
MsgBox("error" & e.Message)
Finally
End Try
Return Nothing
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub bt_guardar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_guardar.Click
Dim resultado As Integer
resultado = cnx_insert2()
MsgBox("los datos se ingresaron correctamente")
End Sub
Protected Sub bt_actualizar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_actualizar.Click
Dim resultado As Integer
resultado = cnx_update2()
MsgBox("el registro se actualizo de forma correcta")
End Sub
Protected Sub bt_borrar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_borrar.Click
Dim resultado As Integer
resultado = cnx_delete2()
MsgBox("el registro se elimino")
End Sub
Protected Sub bt_consultar_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_consultar.Click
Dim resultado As String
resultado = cnx_select2()
text_raza.Text = cnx_select2()
End Sub
End Class
en donde tengo el problema es exactamente en la funcion cnx_select2
si me podrian ayudar se los agradeceria
Valora esta pregunta


0