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)?

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
Sign up to request clarification or add additional context in comments.

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.