Form button:
<asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" />
Code behind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
What is a simple code to insert a value (generated id) into SQL Server database? When
Button1_Click
is triggered, generated id should be inserted totbl_batch
.Should I add anything to
web.config
(like db connection)?
marc_s
759k185 gold badges1.4k silver badges1.5k bronze badges
asked Apr 5, 2015 at 5:44
2 Answers 2
Using con As SqlConnection = New SqlConnection("Server=localhost\SQLEXPRESS; Database=databasename; User Id=sa; Password=yourpassword;")
Using cmd As SqlCommand = con.CreateCommand()
Using da As New SqlDataAdapter
con.Open()
cmd.CommandText = "insert into ..."
da.SelectCommand = cmd
Dim dt As New DataTable
da.Fill(dt)
End Using
End Using
End Using
answered May 13, 2015 at 7:03
user4894464user4894464
Comments
You cannot do CRUD operation in web.config file but I have one simple solution for you using SqlDataSource. Follow up below steps and make your stuff happen, just in simple way.
Connection String In my Web.config:
<add name="Conn" connectionString="Server=David-PC;Database=DNN711;uid=sa;pwd=sa123;" providerName="System.Data.SqlClient"/>
ASPX Markup:
<asp:Button ID="Button1" runat="server" Text="GO" onclick="Button1_Click" />
<asp:SqlDataSource ID="sdsInsert" runat="server"
ConnectionString="<%$ ConnectionStrings:Conn %>"
InsertCommand="Insert into tbl_batch (GeneratedID) values (@GeneratedID)"
InsertCommandType="Text"></asp:SqlDataSource>
Button Click Event:
protected void Button1_Click(object sender, EventArgs e)
{
sdsInsert.InsertParameters.Add("GeneratedID", "123");
sdsInsert.Insert();
}
Please let me know if you have any questions.
answered Apr 5, 2015 at 9:43
Comments
lang-sql