• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 916
  • Last Modified:

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.
0
intchauspe
Asked:
intchauspe
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now