Solved

How to insert string variables in the select statement in the Query Builder?

Posted on 2006-06-30
20
1,270 Views
Last Modified: 2008-01-09
Hello. How to insert string variables in the select statement in the Query Builder? I'm using c# and VS2005

This is how my select looks like when I create directly in my c# code
SELECT [Hyttenavn], [" + cn1 + "], [" + cn2 + "] FROM [GrTilWeb] WHERE [Hyttenavn] = '" + rn + "'"
0
Comment
Question by:m-jansen
  • 13
  • 3
  • 3
  • +1
20 Comments
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17020402
you are building the sting dynamically -  what is this for? to fill a gridview or something like that?
are you using an sql datasource?
0
 

Author Comment

by:m-jansen
ID: 17020484
To fill a Gridview, but my column name and row need to be choosed/changed at runtime. I'm using an ObjectDataSource that is connected to an AccessDataSource.
0
 

Author Comment

by:m-jansen
ID: 17020706
My ObjectDataSource is created like this.

                                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
                                    OldValuesParameterFormatString="original_{0}" SelectMethod="GetHyttePrisVedType1"
                                    TypeName="dbTableAdapters.GrTilWebTableAdapter">
                                    <InsertParameters>
                                        <asp:Parameter Name="Hyttenavn" Type="String" />
                                        <asp:Parameter Name="_1___pris" Type="Double" />
                                        <asp:Parameter Name="Index" Type="Double" />
                                    </InsertParameters>
                                </asp:ObjectDataSource>

Let's say I want to make this field dynamic:
<asp:Parameter Name="Hyttenavn" Type="String" />

Then I guess I must do this in my c# code file

        string mystring = "Hyttenavn";
        ObjectDataSource1 = new ObjectDataSource();
        ObjectDataSource1.InsertParameters.UpdateValues[3] = mystring;

But it does not work. Any suggestions?
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17020772
well, first off, it appears that you only have 3 parameters so the indexing at UpdateValues should be one less then it is, to set a valid value




0
 

Author Comment

by:m-jansen
ID: 17020802
ObjectDataSource1.InsertParameters.UpdateValues[2] = mystring; // Cannot apply indexing with [] to an expression of type 'method group'
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17020820
sorry... it should have been like this..

ObjectDataSource1.InsertParameters["Index"] = mystring;
0
 

Author Comment

by:m-jansen
ID: 17020844
ObjectDataSource1.InsertParameters["Index"] = mystring; // Error      88      Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls.Parameter'
0
 

Author Comment

by:m-jansen
ID: 17020848
I'm using ASP.NET 2.x and VS2005
0
 
LVL 12

Expert Comment

by:topdog770
ID: 17020857
ok.. read it wrong..just a sec!
0
 
LVL 17

Expert Comment

by:Elvio Lujan
ID: 17021113
dim s as string = "SELECT [Hyttenavn], [" & cn1 & "], [" & cn2 &"] FROM [GrTilWeb] WHERE [Hyttenavn] = '" & rn &"'"
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:m-jansen
ID: 17021199
lem2802: that's how I did it (just in c#). Now I created a dataset and created a query there. See?
0
 

Author Comment

by:m-jansen
ID: 17021208
I feel I'm so bad to explain...
0
 

Author Comment

by:m-jansen
ID: 17021221
Have you heard about the Query Builder?
0
 
LVL 12

Expert Comment

by:deanvanrooyen
ID: 17025616
try

        string sindex = "somevalue";
        this.ObjectDataSource1.InsertParameters[0].DefaultValue = sindex;
        this.ObjectDataSource1.InsertParameters[0].Direction = ParameterDirection.Input;
        this.ObjectDataSource1.InsertParameters[0].Type = TypeCode.String;

 I would suggest using the sql datasource and change the datasource to Microsoft Access Database File (OLE DB), I assume
it is a bit easier to work with -   but if you need the object datasource because you have defined your own class to to describe the data layer keep going, good luck...





       
0
 

Author Comment

by:m-jansen
ID: 17026403
The object ObjectDataSource1 is beeing created in the aspx file like this:

                                <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
                                    OldValuesParameterFormatString="original_{0}" SelectMethod="GetHyttePrisVedType1"
                                    TypeName="dbTableAdapters.GrTilWebTableAdapter">
                                    <InsertParameters>
                                        <asp:Parameter Name="Hyttenavn" Type="String" />
                                        <asp:Parameter Name="_1___pris" Type="Double" />
                                        <asp:Parameter Name="Index" Type="Double" />
                                    </InsertParameters>
                                </asp:ObjectDataSource>

It works to change a parameter name like this:

ObjectDataSource1.InsertParameters[0].Name = "asdasdad";
But it won't change the column name and content in the gridview at runtime.
0
 

Author Comment

by:m-jansen
ID: 17026408
protected void Wizard1_Load(object sender, EventArgs e)
    {
        ObjectDataSource1.InsertParameters[2].Name = "asdasdad";
        Label4.Text = ObjectDataSource1.InsertParameters[2].Name;
    }

This is where I try change the ObjectDataSource1
0
 

Author Comment

by:m-jansen
ID: 17026430
deanvanrooyen: I have tried to make my own object of the table I'm going to use and it works good, but I just wondered if I could do the same with a GridView.
0
 
LVL 12

Accepted Solution

by:
deanvanrooyen earned 125 total points
ID: 17028172
why do you need to change the cloumn name, if you rebind the grid it should reflect inserted records? - I just havent used the object datasource yet
0
 

Author Comment

by:m-jansen
ID: 17029408
>why do you need to change the cloumn name, if you rebind the grid it should reflect inserted records?
yes it does
0
 

Author Comment

by:m-jansen
ID: 17029411
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 41
Please explain: Aspect Oriented Programming 2 85
Get Reply from SSIS to intranet site when finished 3 34
Syntax Error 2 47
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now