1

我创建了一个按钮来在文本框中添加字段,我想将文本框名称作为参数传递给子,然后插入到数据库中。我该怎么做。请在 vb 中找到我的代码以供参考。网

在数据库中插入值的代码

Sub Add_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click

        Dim addedButton As Button = sender
        Dim sqlcmd As SqlCommand
        Dim insertdata As String

        addedButton.Text = "Added"
        adduser = True
        addedButton.Enabled = True

        If (cname.Value = " " Or cid.Value = " " Or cadd.Value = " " Or cph.Value = " " Or fax.Value = " " Or cmail.Value = " ") Then
            Message.InnerHtml = "ERROR: Null values not allowed for " _
                & "Client ID, Name"
            Message.Style("color") = "red"
            BindGrid()
        Else
            Message.InnerHtml = "<b>Client Record has been added</b><br/>"     
        End If

        insertdata = "INSERT INTO dbo.ClientInfo([Client Name],[Client ID],[Client Address],[Client Telephone No],[Client Fax No],[Client E-mail]) values("
        insertdata = insertdata + " ' " + cname.Value + " '," + cid.Value + ",' " + cadd.Value + " '," + cph.Value + "," + fax.Value + "," + cmail.Value
        sqlcmd = New SqlCommand(insertdata, sqlcon)



        Try
            sqlcmd.Connection.Open()
            Dim addcount As Integer = sqlcmd.ExecuteNonQuery()

            If addcount > 0 Then
                Message.InnerHtml = "Record successfully Added"
            Else
                Message.InnerHtml = "Record not added"
            End If

        Catch ex As SqlException

            If ex.Number = 2627 Then
                Message.InnerHtml = "ERROR: A record already exists with " _
                   & "the same primary key"
            End If
        Finally
            sqlcmd.Connection.Close()
            BindGrid()
        End Try      
    End Sub


    <asp:Button id="Button1"
                   Text="Add data"
                   OnClick="Add_Click"
                   runat="server"/><br />

  Name:<input type ="text" id="cname" name="" value="" runat="server"/><br />
              ID: <input type = "text" id="cid" name="txtclientid" value="" runat="server"/><br />
              Address:<input type="text" id="cadd" name="txtclientadd" value="" runat="server"/><br />
              Phone No:<input type="text" id="cph" name="txtno" value="" runat="server" /><br />
              Fax No:<input type="text" id="fax" name="faxno" value="" runat="server"/><br />
              E-mail:<input type="text" id="cmail" name="mail" value="" runat="server"/><br />
              <input type="reset" name="reset" value="Clear" /><br />
4

2 回答 2

0

不完全符合您的要求,但更接近您应该做的事情:

Public Sub InsertClientInfo(ByVal ClientName As String, ByVal ClientID As Integer, ByVal ClientAddress As String, ByVal ClientPhone As String, ByVal ClientFax As String, ByVal ClientEmail As String)

    Dim sql As String = _
      "INSERT INTO ClientInfo (" & _
          "[Client Name],[Client ID],[Client Address],[Client Telephone No],[Client Fax No],[Client E-mail]" & _
     ") VALUES (" & _
          "@ClientName,  @ClientID,  @ClientAddress,   @ClientPhone,         @ClientFax,     @ClientEmail)"

    Using cn As New SqlConnection("connection string"), _
          cmd As New SqlCommand(sql, cn)

        'I had to guess at the column types and lengths here. Adjust accordingly
        cmd.Parameters.Add("@ClientName", SqlDbType.NVarChar, 50).Value = ClientName
        cmd.Parameters.Add("@ClientID", SqlDbType.Int).Value = ClientID
        cmd.Parameters.Add("@ClientAddress", SqlDbType.NVarChar, 200).Value = ClientAddress
        cmd.Parameters.Add("@ClientPhone", SqlDbType.NVarChar, 16).Value = ClientPhone
        cmd.Parameters.Add("@ClientFax", SqlDbType.NVarChar, 16).Value = ClientFax
        cmd.Parameters.Add("@ClientEmail", SqlDbType.NVarChar, 50).Value = ClientEmail

        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
End Sub

请注意,我必须猜测数据类型和大小。您需要修复它以制作实际的表定义。像这样称呼它:

InsertClientInfo(textname.Text, Convert.ToInt32(textclientid.Text), txtclientadd.Text, txtno.Text, faxno.Text, mail.Text)
于 2013-01-31T00:44:34.310 回答
0

要将 TEXTBOX 名称传递给任何子,请执行以下操作,

Sub SomeSubName(ThisTextboxName as string)
  'your code here
End Sub

或函数

Function SomeFunctionName(ThisTextBoxName as String) As Boolean
  'your code here
End Function

其中... SomeSubName 和 SomeFunctionName 是您的子程序或函数的名称 ThisTextBoxName 是 STRING 数据的变量名称,它是实际的文本框名称

因此,如果您的文本框名为 txtPhone 那么您会将其传递为....

SomeSubName(txtPhone.Name)

或者

昏暗测试为布尔值

测试 = SomeFunctionName(txtPhone.Name)

于 2013-01-31T00:29:06.213 回答