从/向存储过程检索/添加数据

时间:2014-02-20 09:33:29

标签: sql vb.net html5 data-binding

好吧所以我有这个我必须创建的网页,它有3个页面和一个母版页。现在基本上它都是有效的,除了我通过检索按钮请求数据无法进入网页的事实,也不会更新或添加数据到SQL表。现在在我的查询页面中,当我放入Select * From Customer时,它会将表中的数据带回来以便该部分起作用。根据我提供的PDF格式,他们希望我使用

TextBox1.Text = table.Rows(0).Field<string>("TextBox1")  
TextBox1.DataBind()

要从sql检索数据,现在它会出现错误Overload resolution failed because no accessible 'Field' accepts this number of arguments

所以这是我的客户页面

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data.DataRowCollection

Public Class Customer
Inherits System.Web.UI.Page

Dim conn As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("dbConnection1").ConnectionString)
Shadows adapter As SqlDataAdapter = New SqlDataAdapter()
// Tells me to use Shadows because variable adapter conflicts with property 'adapter'
Dim table As DataTable = New DataTable()
Dim command As SqlCommand = New SqlCommand()

Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
    Try
        conn = New SqlConnection(ConfigurationManager.ConnectionStrings("dbConnection1").ConnectionString)
        Dim command As SqlCommand = New SqlCommand()
        command.Connection = conn
        command.CommandType = CommandType.Text
        command.CommandText = "INSERT INTO Acme_Customer VALUES(" & txtCustID.Text & ",'" & txtFirstname.Text & "',  '" & txtSurname.Text & "', " & txtGender.Text & ",  " + txtAge.Text & ", " & txtAddress1.Text & ", " & txtAddress2.Text & ", " & txtCity.Text & ", " + txtPhone.Text & ", " + txtMobile.Text & ", " & txtEmail.Text & ")"
        command.Connection.Open()
        adapter.InsertCommand = command
        adapter.InsertCommand.ExecuteNonQuery()
        command.Connection.Close()
        Clear()
        lblMessage.Text = "You have been successfully added into our records"
    Catch ex As Exception
        lblMessage.Text = "Something has gone wrong with adding you to our records, please double check everything as we want you to become a member."
    End Try
End Sub


 Protected Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click

    Try
        command.Connection = conn
        command.CommandType = CommandType.StoredProcedure
        command.CommandText = "GetCustomer"
        command.Connection.Open()
        Dim param As SqlParameter = New SqlParameter()
        param.ParameterName = "@ID"
        param.SqlDbType = SqlDbType.Int
        param.Direction = ParameterDirection.Input
        param.Value = txtCustID.Text
        command.Parameters.Add(param)

        adapter.SelectCommand = command
        adapter.Fill(table)

    txtFirstname.Text = table.Rows(0).Field<string>("Firstname")
        txtFirstname.DataBind()
    txtSurname.Text = table.Rows(0).Field<string>("Surname")
        txtSurname.DataBind()
    txtGender.Text = table.Rows(0).Field<String>("Gender")
        txtGender.DataBind()
    txtAge.Text = table.Rows(0).Field<Integer>("Age")
        txtAge.DataBind()
    txtAddress1.Text = table.Rows(0).Field<String>("Address1")
        txtAddress1.DataBind()
    txtAddress2.Text = table.Rows(0).Field<String>("Address2")
        txtAddress2.DataBind()
    txtCity.Text = table.Rows(0).Field<String>("City")
        txtCity.DataBind()
    txtPhone.Text = table.Rows(0).Field<Integer>("Phone Number")
        txtPhone.DataBind()
    txtMobile.Text = table.Rows(0).Field<Integer>("Mobile Number")
        txtMobile.DataBind()
    txtEmail.Text = table.Rows(0).Field<String>("Email")
        txtEmail.DataBind()
   Catch ex As Exception
        lblMessage.Text = "The ID you have entered doesn't exist."
    End Try

End Sub

所以我只是想知道我写错了什么,或者我是否应该使用其他代码而不是我在这里,我知道我应该使用C#但我只是得到了vb的悬念,希望我能在这个项目之后开始使用C#。

1 个答案:

答案 0 :(得分:0)

Protected Sub btnRetrieve_Click(sender As Object, e As EventArgs) Handles btnRetrieve.Click
Try
    command.Connection = conn
    command.CommandType = CommandType.StoredProcedure
    command.CommandText = "GetCustomer"
    command.Connection.Open()
    Dim param As SqlParameter = New SqlParameter()
    param.ParameterName = "@ID"
    param.SqlDbType = SqlDbType.Int
    param.Direction = ParameterDirection.Input
    param.Value = txtCustID.Text
    command.Parameters.Add(param)

    adapter.SelectCommand = command
    adapter.Fill(table)
    dim objDV as dataview = table.defaultview

txtFirstname.Text = objDV(0)("Firstname").tostring
txtSurname.Text = objDV(0)("Surname").tostring
' Fill all other text boxes following above lines

   Catch ex As Exception
        lblMessage.Text = "The ID you have entered doesn't exist."
    End Try

End Sub