Reputation: 5532
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 to tbl_batch
.
Should I add anything to web.config
(like db connection)?
Upvotes: 1
Views: 89
Reputation:
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
Upvotes: 1
Reputation: 1328
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.
Upvotes: 1