1

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
  1. 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.

  2. 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

1
 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

Comments

1

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.