InsertParameters

  • Thread starter Thread starter MC
  • Start date Start date
M

MC

Hi all

Complete newby trying to understand something

I have made my own form with some fields and then dragged a SqlDataSource
onto the page and made an Insert to go with the SqlDataSource.

I have a button which calls this

Protected Sub InsertTextBox_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles InsertTextBox.Click

Me.SqlDataSource1.InsertParameters.Add("@CompanyID",
Me.CompanyIDTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@ContactName",
Me.ContactNameTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@CompanyName",
Me.CompanyNameTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@Postcode", Me.PostCodeTextBox.Text)

Try

Me.SqlDataSource1.Insert()

Catch ex As Exception

Response.Write(ex.Message)

End Try

End Sub

I have checked parameter names etc put it wont put the values to the
parameters. Obviously the .Add is wrong, what should be using?

Cheers
 
MC said:
Me.SqlDataSource1.InsertParameters.Add("@Postcode",
Me.PostCodeTextBox.Text)
I have checked parameter names etc put it wont put the values to the
parameters. Obviously the .Add is wrong, what should be using?

Add is correct, but it doesn't want to tell you it needs to know the data
type (e.g. SqlDbType.NVarChar) and, for strings, the length.

Andrew
 
Thanks Andrew,

So whats the syntax?

MC

Andrew Morton said:
Add is correct, but it doesn't want to tell you it needs to know the data
type (e.g. SqlDbType.NVarChar) and, for strings, the length.

Andrew
 
Depending on what your insert command is called, if you used a
SqlCommand object then it will go like this...

SqlCommand1.Parameters("@CompanyName").Value = CompanyNameTextBox.Text
 
I must be doing something totally wrong. ADO is confussing me but I am
getting there.

OK and using V2 .net. I could have used a formview but choose not to cause
it does some validation on fields as I go and it was to confusing using the
formview.

So I made my own form and I get the validations working fine.

I hit insert button i created and then with SqlDataSource1 I dragged on to
the webpage (with Insert Command in toe) i created the following procedure.

Protected Sub InsertTextBox_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles InsertTextBox.Click

Me.SqlDataSource1.InsertParameters.Add("@CompanyID", Me.CompIDTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@ContactName",
Me.ContactNameTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@CompanyName",
Me.CompanyNameTextBox.Text)

Me.SqlDataSource1.InsertParameters.Add("@Postcode", Me.PostCodeTextBox.Text)

Try

Me.SqlDataSource1.Insert()

Catch ex As Exception

Response.Write(ex.Message)

End Try

End Sub


Which as you know I listed earlier. The Insert Command is as shown below:

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:connEducare %>"

DeleteCommand="DELETE FROM [Companies] WHERE [ID] = @original_ID"

InsertCommand="INSERT INTO [Companies] ([CompanyID], [ContactName],
[TelNumber], [FaxNumber], (e-mail address removed)
 
First try changing this line to...

Me.SqlDataSource1.InsertParameters.Add("@CompanyID", 32)

Or some other Integer that doesnt violate the database constraints.
 
MC said:
Thanks Andrew,

So whats the syntax?

I do it the long way round, e.g:-

Dim sqlCmd As New SqlCommand("AddBasket", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure

Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Char, 36)
sqlParam.Value = myUserID
sqlCmd.Parameters.Add(sqlParam)

sqlParam = New SqlParameter("@BasketName", SqlDbType.NVarChar, 24)
sqlParam.Value = basketName
sqlCmd.Parameters.Add(sqlParam)

' etc.

Andrew
 
Great example Andrew,
I also use the same method you do. IMHO, I think it is a little
cleaner than the way VS adds things.
 
MC,

It is not so important what you use, if you use the parameters only once,
than you can use your method.

If you will use them more by instance as you do, than at the second button
click you are in trouble, because it is a collection, so you have than 8
etc. parameters while 4 are expected.

Therefore the methode from Andrew has a little bit to be changed..

Form Load event.
Dim sqlCmd As New SqlCommand("AddBasket", sqlConn)
sqlCmd.CommandType = CommandType.StoredProcedure
Dim sqlParam As New SqlParameter("@UserID", SqlDbType.Char, 36)
sqlCmd.Parameters.Add(sqlParam)
sqlParam = New SqlParameter("@BasketName", SqlDbType.NVarChar, 24)
sqlCmd.Parameters.Add(sqlParam)

Button Event
sqlCmd.Parameters("@UserID").Value = myUserID
sqlCmd.Parameters("@BasketName").Value = basketName

I hope this gives an idea.

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top