2014-02-20 37 views
0

好了,所以我有这个网页,我要创造,它有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#。

+0

啊! SQL注入的好机会在'btnAdd_Click' – equisde

+0

“我知道我应该使用C#” - 为什么?没有“应该”参与。如果你想使用C#,现在就去学习吧。如果你想使用C#,学习VB没有意义。相反,VB也完全可以。他们只是同一种语言的两种方言。事实上,上面给出的代码在C#中几乎是相同的。 – peterG

回答

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 
+0

试过了,我得到异常,试图将参数从字符串转换为int。在'adapter.Fill(table)是它崩溃的地方。不太清楚它是否意味着要参与,因为你有Dataview在那里,但我想不出要改变它。 – ChokeOnThis

相关问题