Advertisement

04.15.2008 at 09:02AM PDT, ID: 23324168
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

9.0

Unable to Add Parameter to SqlConnection calling a SP

Asked by Thirt in .Net Editors & IDEs, MS SQL Server, Visual Studio

Not exactly sure which section to post this, so please forgive.  However, I have a store procedure that generates a crosstab query result. So using the normal Gridview Data source editor to assign it a Store Procedure doesn't work...since the gridview has no idea what the scheme will look like. So I need to find a way to generate a gridview dynamically.
1. My first code snippet shows the solution I'm playing with. Works well on a SP that provides static columns. Is there a better way to dynamically generate a gridview?
2. Trying to modify the code to pass in the paramters the crosstab store procedure needs. Snippette attached below. I get the follow error and not sure how to trouble shoot this. Any suggestions on how to trouble shoot or do better welcomed. Thanks!!

Error:
System.Data.SqlClient.SqlException was unhandled by user code
  Class=15
  ErrorCode=-2146232060
  LineNumber=1
  Message="Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Incorrect syntax near the keyword 'add'. Line 1: Incorrect syntax near '('. Line 23: Incorrect syntax near ','."
  Number=156
  Procedure=""
  Server="server8"
  Source=".Net SqlClient Data Provider"
  State=1
  StackTrace:
       at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)    at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)    at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()    at System.Data.SqlClient.SqlDataReader.get_MetaData()    at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)    at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)    at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)    at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)    at System.Data.SqlClient.SqlCommand.ExecuteReader()    at DisciplineEntry2.Button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\thirt\Personal\Visual Studio 2008\WebSites\EE_DisciplineEntry\DisciplineEntry2.aspx.vb:line 60    at System.Web.UI.WebControls.Button.OnClick(EventArgs e)    at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)    at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)    at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)    at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
*****First Snippet*****
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cs As String = "Data Source=server8;Initial Catalog=PP_Estimator;Integrated Security=True"
 
        Using con As New System.Data.SqlClient.SqlConnection(cs)
            con.Open()
            Dim cmd As New System.Data.SqlClient.SqlCommand()
            cmd.Connection = con
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "test4ee"
 
            Using reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
                GridView1.DataSource = reader
                GridView1.DataBind()
 
            End Using
 
        End Using
    End Sub
*****end First snippet****
 
****Second snippett*****
 Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cs As String = "Data Source=server8;Initial Catalog=PP_Estimator;Integrated Security=True"
 
        Using con As New System.Data.SqlClient.SqlConnection(cs)
            con.Open()
            Dim cmd As New System.Data.SqlClient.SqlCommand()
            cmd.Connection = con
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "sys_CrossTab"
 
            cmd.Parameters.Add("@SQLSource", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@SQLSource").Value = "estimates INNER JOIN tasks ON estimates.estimate_id = tasks.estimate_id INNER JOIN tlkpActivityMicroCodes ON tasks.discipline_code = tlkpActivityMicroCodes.flddisciplinecode AND tasks.activity_code = tlkpActivityMicroCodes.fldactivitycode AND tasks.activity_subcode = tlkpActivityMicroCodes.fldactivitysubcode AND tasks.activity_microcode = tlkpActivityMicroCodes.fldactivitymicrocode INNER JOIN tlkpActivitySubCodes ON tlkpActivityMicroCodes.flddisciplinecode = tlkpActivitySubCodes.flddisciplinecode AND tlkpActivityMicroCodes.fldactivitycode = tlkpActivitySubCodes.fldactivitycode AND tlkpActivityMicroCodes.fldactivitysubcode = tlkpActivitySubCodes.fldactivitysubcode INNER JOIN tlkpActivityCodes ON tlkpActivitySubCodes.flddisciplinecode = tlkpActivityCodes.flddisciplinecode AND tlkpActivitySubCodes.fldactivitycode = tlkpActivityCodes.fldactivitycode INNER JOIN tlkpDisciplineCodes ON tlkpActivityCodes.flddisciplinecode = tlkpDisciplineCodes.flddisciplinecode INNER JOIN effort ON tasks.task_id = effort.task_id"
 
            cmd.Parameters.Add("@ColFieldID", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@ColFieldID").Value = "effort.position"
 
            cmd.Parameters.Add("@ColFieldName", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@ColFieldName").Value = "effort.position"
 
            cmd.Parameters.Add("@ColFieldOrder", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@ColFieldOrder").Value = "effort.position"
 
            cmd.Parameters.Add("@CalcFieldName", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@CalcFieldName").Value = "effort.qty * effort.hours"
 
            cmd.Parameters.Add("@RowFieldNames", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@RowFieldNames").Value = "tasks.task_id,tasks.discipline_code, tasks.activity_code, tasks.activity_subcode, tasks.activity_microcode, tlkpDisciplineCodes.flddisciplinedescription, tlkpActivityCodes.fldactivitydescription, tlkpActivitySubCodes.fldactivitysubcodedescription, tlkpActivityMicroCodes.fldactivitymicrocodedescription, effort.qty"
 
            cmd.Parameters.Add("@TempTableName", Data.SqlDbType.VarChar, 200)
            cmd.Parameters("@TempTableName").Value = ""
 
            cmd.Parameters.Add("@CalcOperation", Data.SqlDbType.VarChar, 50)
            cmd.Parameters("@CalcOperation").Value = "sum"
 
            cmd.Parameters.Add("@Debug", Data.SqlDbType.Bit)
            cmd.Parameters("@Debug").Value = 0
 
            cmd.Parameters.Add("@SourceFilter", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@SourceFilter").Value = "estimates.estimate_id = 649 and tasks.project_id=2415 and tasks.discipline_code = 9"
 
            cmd.Parameters.Add("@NumColOrdering", Data.SqlDbType.Bit)
            cmd.Parameters("@NumColOrdering").Value = 0
 
            cmd.Parameters.Add("@RowTotals", Data.SqlDbType.VarChar, 100)
            cmd.Parameters("@RowTotals").Value = "Total"
 
            cmd.Parameters.Add("@ColTotals", Data.SqlDbType.VarChar, 100)
            cmd.Parameters("@ColTotals").Value = ""
 
            cmd.Parameters.Add("@OrderBy", Data.SqlDbType.VarChar, 8000)
            cmd.Parameters("@OrderBy").Value = "discipline_code, activity_code"
 
            cmd.Parameters.Add("@CalcFieldType", Data.SqlDbType.VarChar, 100)
            cmd.Parameters("@CalcFieldType").Value = "int"
 
            Using reader As System.Data.SqlClient.SqlDataReader = cmd.ExecuteReader()
                GridView1.DataSource = reader
                GridView1.DataBind()
 
            End Using
 
 
        End Using
    End Sub
[+][-]04.15.2008 at 01:10PM PDT, ID: 21362369

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 01:33AM PDT, ID: 21365770

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]04.16.2008 at 06:55AM PDT, ID: 21367748

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.16.2008 at 06:58AM PDT, ID: 21367786

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.17.2008 at 08:45AM PDT, ID: 21378391

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .Net Editors & IDEs, MS SQL Server, Visual Studio
Sign Up Now!
Solution Provided By: tkreimer
Participating Experts: 2
Solution Grade: A
 
 
[+][-]04.17.2008 at 09:51AM PDT, ID: 21379064

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 - Hierarchy / EE_QW_2_20070628