Showing posts with label button. Show all posts
Showing posts with label button. Show all posts

Sunday, March 11, 2012

2nd INSERT INTO statement in a Button click event

Hi everyone.
I am trying to write 2 INSERT INTO statements into a Button click event. Both statements go to the same database but differnet tables. The first statement works fine but the 2nd causes an error with the Try, Catch, Finally statement. When I remove the ExecuteNonQuery from the 2nd statement, the 2nd INSERT INTO statement fails to work. Any help would be brilliant. Thanks!

Private Sub btnInsertChange_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles btnInsertChange.Command

'Insert Guide changes into ChangesReport table in sql server
Me.SqlCommandThemeTest.Connection = Me.SqlConnection1

Dim Name As String
Dim values As String
Dim sSQL As String
Name = "Theme, Guide, GuidePage, PageType, ChangeCategory, ChangeFrom, ChangeFromText, ChangeTo, ChangeToText ContentManager"
values = "lstTheme, lstGuideName, lstGuidePage, lstPageType, lstChangeCategory, lstChangeFrom, txtChangeFrom, lstChangeTo, txtChangeTo, Label1"
SqlCommandThemeTest.CommandText = "INSERT INTO dbo.ChangesReport (Theme, Guide, GuidePage, PageType, ChangeCategory, ChangeFrom, ChangeFromText, ChangeTo, ChangeToText, ContentManager) VALUES (@.themeValue, @.guideValue, @.guidepageValue, @.pagetypeValue, @.changecategoryValue, @.changefromValue, @.changefromtextValue, @.changetoValue, @.changetotextValue, @.contentmanagerValue)"

SqlCommandThemeTest.Parameters.Add("@.themeValue", lstTheme.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.guideValue", lstGuideName.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.guidepageValue", lstGuidePage.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.pagetypeValue", lstPageType.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.changecategoryValue", lstChangeCategory.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.changefromValue", lstChangeFrom.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.changefromtextValue", txtChangeFrom.Text)
SqlCommandThemeTest.Parameters.Add("@.changetoValue", lstChangeTo.SelectedItem.Text)
SqlCommandThemeTest.Parameters.Add("@.changetotextValue", txtChangeTo.Text)
SqlCommandThemeTest.Parameters.Add("@.contentmanagerValue", Label1.Text)

Try
Me.SqlConnection1.Open()
Me.SqlCommandThemeTest.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.ToString)
Finally
Me.SqlConnection1.Close()
End Try


'Insert textbox to ChangeLogFrom ddl
Me.CmdDDLFromUpdate.Connection = Me.SqlConnection1

Name = "ChangeFromText"
values = "txtChangeFrom"
sSQL = "INSERT INTO dbo.Change Log From (ChangeLogFrom) VALUES (@.changelogfromValue)"
Me.CmdDDLFromUpdate.Parameters.Add("@.changelogfromValue", txtChangeFrom.Text)

Try
Me.SqlConnection1.Open()
Me.CmdDDLFromUpdate.ExecuteNonQuery()
Catch ex As Exception
Response.Write(ex.ToString)
Finally
Me.SqlConnection1.Close()
End Try

I have tried various different things with these 2 statements but can still only get the first to insert.

Any advice would be great. Thanks

|||

It's hard to tell since you've only cut and pasted certain parts of the code. For example, I don't see where you've set the commandtext of the second sqlcommand. I see the sSQL string being set, but that isn't the same thing.

You might also want the keep the sqlconnection open for both updates, no use in closing it then reopening it.

|||Why dont you pass all the parameters to a stored proc and do both the inserts there. That will save you a round trip and your code will be at one place.

Sunday, February 19, 2012

2005 Management Studio Reformats my source!

Minor issues that are driving me crazy about this tool:
1. I type in a new view and format it the way it makes sense to me. I hit
the Save button, and SqlMS decides it wants to format it a completely
different way. How can I turn this feature off, or tell the program how I
want it to format my code? Older versions of EM did not reformat the code.
2. When I create a new view, the Diagram, Results and Criteria panes
automatically display. I have to click the buttons to hide them each time.
I don't need them or want them taking up editing space. How can I make the
program remember that I don't want it to display these "helpful" windows
each time?
Please advise
no help or resolution here?
"ZippyThePinhead" wrote:

> Minor issues that are driving me crazy about this tool:
> 1. I type in a new view and format it the way it makes sense to me. I hit
> the Save button, and SqlMS decides it wants to format it a completely
> different way. How can I turn this feature off, or tell the program how I
> want it to format my code? Older versions of EM did not reformat the code.
> 2. When I create a new view, the Diagram, Results and Criteria panes
> automatically display. I have to click the buttons to hide them each time.
> I don't need them or want them taking up editing space. How can I make the
> program remember that I don't want it to display these "helpful" windows
> each time?
> Please advise
>
|||Why not create the view as a CREATE VIEW statement inside of a Query window? I
don't seem to have any issues when I do that. Of course, you will need to
choose ALTER - Script to New Window/Clipboard in order to modify the code
instead of choosing Modify as Modify defaults to that view.
-Pete Schott
ZippyThePinhead <ZippyThePinhead@.discussions.microsoft.com> wrote:
[vbcol=seagreen]
> no help or resolution here?
> "ZippyThePinhead" wrote:
|||Thanks Peter -- yes that certainly does work, but it's a pain in the rear to
have to manage this myself..which is what I have been doing lately.
There simply needs to be some options available in the program to stop the
auto-formatting of our code. I've noticed that sometimes it even messes
things up by adding "as exp1" expressions on columns the parser thinks are
misnamed!
"Peter A. Schott" wrote:

> Why not create the view as a CREATE VIEW statement inside of a Query window? I
> don't seem to have any issues when I do that. Of course, you will need to
> choose ALTER - Script to New Window/Clipboard in order to modify the code
> instead of choosing Modify as Modify defaults to that view.
> -Pete Schott
> ZippyThePinhead <ZippyThePinhead@.discussions.microsoft.com> wrote:
>