We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now


SQL Insert with ASP.NET 2.0

intchauspe asked
Medium Priority
Last Modified: 2008-01-09
Hi guys,

I have what I think is a pretty easy question.  I've just started using ASP and I'm trying to take the input from 2 text boxes and run an insert command when a button is pushed.    I have the text boxes set as parameters and the query seems to work but I need to know how to make it fire when the "add" button is pushed.  I assume I need to add some code to the click event of the button but everything I've tried so far isn't working.  

I already have a SqlDataSource that displays (selects) the current records  I also defined the insert command and the parameters.  how to I connect that button event to the insert command on the SqlDataSource?

 Can someone point me down the right path.
Watch Question

the sqldatasource insert command doesn't really work well with custom buttons. you're better off keeping your custom button and just writing the procedure for inserting the record in your OnClick event of the button. you should have something like:

Sub Button1_Clicked(ByVal Sender as Object, ByVal e as ButtonCommandEventArgs)
    Dim strInsert as String = "your insert command here with @parameters in the string"
    Dim objCmd as New SqlCommand(strInsert, objConn)   'objConn is your predefined connection string
    objCmd.Parameters.Add("@ParameterName", SqlDbType.VarChar).Value = TextBox1.Text
              ----if this was supposed to be an integer, you'd need to replace the line above with
    objCmd.Parameters.Add("@ParameterName", SqlDbType.Int).Value = CInt(TextBox1.Text)
    'rebind the datagrid/gridview here
End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Raju SrivatsavayeSoftware Engineer

dim strSql as string
strSql= me.SqlDataSource1.InsertCommand
strSql.Parameters.Add("@ParameterName", SqlDbType.VarChar).Value = TextBox1.Text


This is when you have an insert command generated along with your sqldatasource..

See if this works


Thanks Craskin - That worked Great.  I knew it should be simple. I was just a little foggy on the details. Thanks for putting me on the path!
Hi, I have a similar issue, although I have a stored procedure ready to accept parameters for an insert based on what I assume would be the onclick event of my button.

As I am very new to ASP.Net 2.0, any help anyone has on this issue would be a great help.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.