DataSourceID ASP.NET - c#

Greetings i have this Gridview, ASP.NET has a Wizard to bind Data to Gridview, it gives you the TSQL query in an asp.net tag, but i was wondering how to do it in C# in code-behind.
HTML:
<asp:SqlDataSource ID="SqlDataSourceMain" runat="server"
ConnectionString="<%$ ConnectionStrings:Laptop %>" SelectCommand="SELECT [fCodeProducts],
[fCodeGroup], [fName], [fPrice], [fImageName],
[fDesc], [fMojoodi], [Namayesh],
[FileAddress] FROM tProducts WHERE (fCodeGroup = 12)
OR (fCodeGroup = #fCodeGroup) AND (Namayesh = 'True')
ORDER BY fCodeGroup">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue="200" Name="fCodeGroup" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>

If you need to change the SqlDataSource on code behind, you can do it like this:
SqlDataSourceMain.SelectCommand = "Select * from tProducts where Id=#MyParameter";
SqlDataSourceMain.SelectParameters["MyParameter"].DefaultValue = 1;
SqlDataSourceMain.DataBind();
You use it the exact same way, just bind it and bind it to your control.
Here is an Example that goes into more depth.

Related

Can I use the update statement with the select statement together in an SqlDataSource?

My query looks like this:
<asp:SqlDataSource ID="UpdateFullNameSQL" runat="server" ConnectionString="<%$ ConnectionStrings:UserQueries %>" ProviderName="<%$ ConnectionStrings:UserQueries.ProviderName %>"
UpdateCommand="update users set firstname = :changefirstname, lastname = :changelastname where username = :currentusername">
<UpdateParameters>
<asp:ControlParameter ControlID="ChangeFirstNameBox" Name="changefirstname" PropertyName="Text" Type="Empty" />
<asp:ControlParameter ControlID="ChangeLastNameBox" Name="changelastname" PropertyName="Text" Type="Empty" />
</UpdateParameters>
<SelectParameters>
<asp:ControlParameter ControlID="UsernameBox" Name="currentusername" PropertyName="Text" Type="Empty" />
</SelectParameters>
</asp:SqlDataSource>
So I have two parameters that I want to update from and one parameter that I want to use to change the data where it matches withthe selectparameter's data. And when I want to execute it, it shows an ORA-01008 exception.
My code-behind only has a updatefullnamesql.update(); function.
What am I missing here/doing wrong?
In update command you should use # instead of :. I.E.:
UpdateCommand="update users set firstname = #changefirstname,
lastname = #changelastname where username = #currentusername;"
Additionally you have to specify the #currentusername as update parameter
<UpdateParameters>
...
<asp:ControlParameter ControlID="UsernameBox"
Name="currentusername" PropertyName="Text" Type="Empty" />
</UpdateParameters>

How to use controls value in mysql query

I have a database with the tables and columns:
persons: id, first_name, last_name
addresses: id, city, street_number, persons_id
I'm trying to make a search engine in asp.net.
I have controls: TextBox1, DropDownList1, Button1, GridView1 with SqlDataSource1.
The DropDownList1 code:
<asp:DropDownList ID="DropDownList1" runat="server"
DataValueField="first_name" AutoPostBack="false">
<asp:ListItem Value="first_name">First name</asp:ListItem>
<asp:ListItem Value="last_name">Last name</asp:ListItem>
<asp:ListItem Value="city">City</asp:ListItem>
</asp:DropDownList>
In the SqlDataSource control: in the SelectCommand I'm trying to make something like this:
SelectCommand="SELECT first_name, last_name, city FROM persons, addresses WHERE persons.id = addresses.persons_id AND (? LIKE '%'??'%')">
In the ? and ?? I want to put the values from DropDownList1 and TextBox1. Any suggestions ?
You will need two asp:ControlParameter's in your SqlDataSource1 like this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="YourConnectionString"
SelectCommand="SELECT first_name, last_name, city FROM persons, addresses WHERE persons.id = addresses.persons_id AND (#FirstLastCity LIKE '%#SearchString%')">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="FirstLastCity" PropertyName="SelectedValue" />
<asp:ControlParameter ControlID="TextBox1" Name="SearchString" PropertyName="Text" />
</SelectParameters>
</asp:SqlDataSource>
This should be the first step. I am pretty sure that '%#SearchString%' in the SelectCommand will not work. But there must be a way to concatinate %, #SearchString and % in the markup code.
Alternatively you can set parameter values in code behind like this:
SqlDataSource1.SelectParameters.Add("#SearchString", "%" + TextBox1.Text + "%");

load column data into checkboxlist

i am trying to load r1,r2,r3 column to a checkboxlist, but the output is not my expected result
data inside my db
Question r1 r2 r3
who am i? A B C
output(item that inside my checkboxlist)
A
Expected output
A
B
C
i was tried to add r1,r2,r3 into DataTextField but asp.net not allow me to do so=(
DataSourceID="SqlDataSource1" DataTextField="r1" DataValueField="r1">
</asp:CheckBoxList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [r1], [r2], [r3] FROM [ExerciseTable] WHERE ([Question] = #Question)">
<SelectParameters>
<asp:Parameter DefaultValue="who am i?" Name="Question" Type="String" />
</SelectParameters>
Try this as the select command:
SELECT [r1] AS [Option] FROM [ExerciseTable] WHERE ([Question] = #Question)
UNION
SELECT [r2] AS [Option] FROM [ExerciseTable] WHERE ([Question] = #Question)
UNION
SELECT [31] AS [Option] FROM [ExerciseTable] WHERE ([Question] = #Question)

Customize SqlDataSource to store part of string

I'm using an UpdatePanel in ASP.Net WebForms to upload a image. I set the Image.ImageUrl to the full virtual path to the image (/images/news/filenname.jpg), but I only want to save the filename in the database (filename.jpg). How do I customize the SqlDataSource to only take part of the URL?
<asp:SqlDataSource
ID="SqlDataSourceNews"
runat="server"
ConnectionString="<%$ ConnectionStrings:connectionString %>"
UpdateCommand="UPDATE [foo] SET [pic] = #pic WHERE [id] = #id">
<UpdateParameters>
<asp:Parameter Name="pic" Type="String" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="pic" Type="String" />
</InsertParameters>
</asp:SqlDataSource>
You can set the update parameters on page_load and use substring function to get only file name from full url:
protected void Buton_Click(object sender, EventArgs e)
{
SqlDataSource1.UpdateParameters["pic"].DefaultValue = "string fetched from substring method";
SqlDataSource1.Update();
}

Converted from SqlDataSource to ObjectDataSource, causing grief

I have created a data access layer in my web app which uses ObjectDataSource instead of SqlDataSource. I have a FormView to update some data in my database. In my old asp.net code I had something like:
<asp:SqlDataSource ID="sdsTradeDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:ForexDB %>"
SelectCommand="usp_GetTrade" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateTrade" UpdateCommandType="StoredProcedure"
<SelectParameters>
<asp:ControlParameter Name="tradeId" ControlID="grdTrades" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="tradeId" ControlId="frmTrade" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>
Which worked fine. I have replaced the SqlDataSource with this:
<asp:ObjectDataSource
id="srcTrade"
TypeName="DatabaseComponent.DBUtil"
SelectMethod="GetTrade"
UpdateMethod="UpdateTrade"
runat="server">
<SelectParameters>
<asp:QueryStringParameter Name="tradeId" QueryStringField="tradeId" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="tradeId" ControlId="frmTrade" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:ObjectDataSource>
But now I get this error when I click the Update button in my FormView:
Exception Details:
System.InvalidOperationException:
ObjectDataSource 'srcTrade' could not
find a non-generic method
'UpdateTrade' that has parameters:
symbol, pctAccountRisked,
tradeSetupId, lotsPerUnit,
initialStopPrice, tfCode, MAEPips,
MFEPips, tradeGrade, executionGrade,
tradeTypeId, comment, tradeId.
In my DBUtil class I have this for UpdateTrade:
public void UpdateTrade(
int tradeId,
string symbol,
decimal pctAccountRisked,
string tradeSetupId,
decimal lotsPerUnit,
decimal initialStopPrice,
string tfCode,
int MAEPips,
int MFEPips,
int tradeGrade,
int executionGrade,
string comment)
{
SqlCommand cmd = new SqlCommand("usp_UpdateTrade");
cmd.Parameters.AddWithValue("#tradeId", tradeId);
cmd.Parameters.AddWithValue("#symbol", symbol);
cmd.Parameters.AddWithValue("#pctAccountRisked", pctAccountRisked);
cmd.Parameters.AddWithValue("#tradeSetupId", tradeSetupId);
cmd.Parameters.AddWithValue("#lotsPerUnit", lotsPerUnit);
cmd.Parameters.AddWithValue("#initialStopPrice", initialStopPrice);
cmd.Parameters.AddWithValue("#tfCode", tfCode);
cmd.Parameters.AddWithValue("#MAEPips", MAEPips);
cmd.Parameters.AddWithValue("#MFEPips", MFEPips);
cmd.Parameters.AddWithValue("#tradeGrade", tradeGrade);
cmd.Parameters.AddWithValue("#executionGrade", executionGrade);
cmd.Parameters.AddWithValue("#comment", comment);
UpdateTable(cmd, "trade");
}
and this for GetTrade:
public DataTable GetTrade(int tradeId)
{
SqlCommand cmd = new SqlCommand("usp_GetTrade");
cmd.Parameters.AddWithValue("#tradeId", tradeId);
return FillDataTable(cmd, "trade");
}
Please help!
Hi your UpdateTrade method and the passing parameters from your datasource are missmatching. please recheck them

Categories