我创建了一个按钮来在文本框中添加字段,我想将文本框名称作为参数传递给子,然后插入到数据库中。我该怎么做。请在 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 />