我创建了一个按钮,用于在文本框中添加字段,我想将文本框名称作为参数传递到子文件中,然后插入到数据库中。我该怎么做..请找我的代码供参考在vb.net下面
在数据库中插入值的代码
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 />
答案 0 :(得分:0)
要将TEXTBOX名称传递给任何sub,请执行以下操作,
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)
或
Dim Test as Boolean
Test = SomeFunctionName(txtPhone.Name)
答案 1 :(得分: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)