m-jansen
asked on
How to insert string variables in the select statement in the Query Builder?
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 + "'"
This is how my select looks like when I create directly in my c# code
SELECT [Hyttenavn], [" + cn1 + "], [" + cn2 + "] FROM [GrTilWeb] WHERE [Hyttenavn] = '" + rn + "'"
ASKER
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.
ASKER
My ObjectDataSource is created like this.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
OldValuesParameterFormatSt ring="orig inal_{0}" SelectMethod="GetHyttePris VedType1"
TypeName="dbTableAdapters. GrTilWebTa bleAdapter ">
<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.InsertPa rameters.U pdateValue s[3] = mystring;
But it does not work. Any suggestions?
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
OldValuesParameterFormatSt
TypeName="dbTableAdapters.
<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.InsertPa
But it does not work. Any suggestions?
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
ASKER
ObjectDataSource1.InsertPa rameters.U pdateValue s[2] = mystring; // Cannot apply indexing with [] to an expression of type 'method group'
sorry... it should have been like this..
ObjectDataSource1.InsertPa rameters[" Index"] = mystring;
ObjectDataSource1.InsertPa
ASKER
ObjectDataSource1.InsertPa rameters[" Index"] = mystring; // Error 88 Cannot implicitly convert type 'string' to 'System.Web.UI.WebControls .Parameter '
ASKER
I'm using ASP.NET 2.x and VS2005
ok.. read it wrong..just a sec!
dim s as string = "SELECT [Hyttenavn], [" & cn1 & "], [" & cn2 &"] FROM [GrTilWeb] WHERE [Hyttenavn] = '" & rn &"'"
ASKER
lem2802: that's how I did it (just in c#). Now I created a dataset and created a query there. See?
ASKER
I feel I'm so bad to explain...
ASKER
Have you heard about the Query Builder?
try
string sindex = "somevalue";
this.ObjectDataSource1.Ins ertParamet ers[0].Def aultValue = sindex;
this.ObjectDataSource1.Ins ertParamet ers[0].Dir ection = ParameterDirection.Input;
this.ObjectDataSource1.Ins ertParamet ers[0].Typ e = 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...
string sindex = "somevalue";
this.ObjectDataSource1.Ins
this.ObjectDataSource1.Ins
this.ObjectDataSource1.Ins
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...
ASKER
The object ObjectDataSource1 is beeing created in the aspx file like this:
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
OldValuesParameterFormatSt ring="orig inal_{0}" SelectMethod="GetHyttePris VedType1"
TypeName="dbTableAdapters. GrTilWebTa bleAdapter ">
<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.InsertPa rameters[0 ].Name = "asdasdad";
But it won't change the column name and content in the gridview at runtime.
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" InsertMethod="Insert"
OldValuesParameterFormatSt
TypeName="dbTableAdapters.
<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.InsertPa
But it won't change the column name and content in the gridview at runtime.
ASKER
protected void Wizard1_Load(object sender, EventArgs e)
{
ObjectDataSource1.InsertPa rameters[2 ].Name = "asdasdad";
Label4.Text = ObjectDataSource1.InsertPa rameters[2 ].Name;
}
This is where I try change the ObjectDataSource1
{
ObjectDataSource1.InsertPa
Label4.Text = ObjectDataSource1.InsertPa
}
This is where I try change the ObjectDataSource1
ASKER
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>why do you need to change the cloumn name, if you rebind the grid it should reflect inserted records?
yes it does
yes it does
ASKER
thanks
are you using an sql datasource?