SQL Insert with ASP.NET 2.0

Posted on 2006-03-20
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.
Question by:intchauspe
    LVL 12

    Accepted Solution

    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
    LVL 9

    Expert Comment

    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

    Author Comment

    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!

    Expert Comment

    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.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
    In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now