Question:
Insert Data to SQL from TextBox?
bluezonemd
2008-05-27 05:02:00 UTC
Can someone help me with this in VB.net. I am trying to insert Data from textBox into a table in SQL 2005.
I have tried something like this, but it won't reconize SQL connection? How can I also repost the data after ONButton Click on the same page to the datagrid?

OnButton Click

Dim sqlStmt As String
Dim conString As String
Dim cn As Sqlconnection (Won't reconize SQL connection)
Dim cmd As SqlCommand (Won't reconize SQL command)
Try
sqlStmt = "insert into Table1 (FName) Values (@Fname) "
conString = "Server and connection here is correct;"
cn = New SqlConnection(conString)
cmd = New SqlCommand(sqlStmt, cn)
cmd.Parameters.Add(New SqlParameter("@FName", SqlDbType.NVarChar, 11))
cmd.Parameters("@FName").Value = TxtBoxNewRN.Text
cn.Open()
cmd.ExecuteNonQuery()
Finally
cn.Close()
End Try
Three answers:
David K
2008-05-27 05:25:26 UTC
make sure you have

Imports System.Data.SqlClient



with your other imports.
?
2016-12-08 16:18:03 UTC
that's an occasion to persist with: sq. = "insert into product (Product_id,Product_name,Product_price) values(7,'Product7',seven hundred)" attempt connection.Open() adapter.InsertCommand = New SqlCommand(sq., connection) adapter.InsertCommand.ExecuteNonQuery(... MsgBox("Row inserted !! ") seize ex As Exception MsgBox(ex.ToString) end attempt replace the numbers in values with your text textile field values.
SakuiWeb.com
2008-05-27 05:10:27 UTC
don't use direct datalinks, it's easyer to use and execute query statments.



the rough code would be:

[update table set fname = ']+textbox1.text+[' where counter = ]+lnrow+[ ]


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...