SQL Insert with ASP.NET 2.0

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.
intchauspeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

craskinCommented:
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)
   
    objConn.Open()
    objCmd.ExecuteNonQuery()
    objConn.Close()
   
    'rebind the datagrid/gridview here
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Raju SrivatsavayeSoftware EngineerCommented:
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
0
intchauspeAuthor Commented:
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!
0
paul24_ukCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.