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)
Related
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.
I am trying to delete data of selected row in gridview but I am getting error like this
"Must declare the scalar variable "#ModelID"."
here's my code of sqldataadapter
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString='<%$ ConnectionStrings:myconnectionstring %>' SelectCommand="SELECT * FROM [ChannelsDataValidation] WHERE ([ModelID] = #ModelID)" DeleteCommand="DELETE FROM ChannelsDataValidation WHERE (ModelID = #ModelID)" UpdateCommand="UPDATE ChannelsDataValidation SET ChannelNumber = #ChannelNumber, DataType = #DataType, MinValue = #MinValue, MaxValue = #MaxValue, Unit = #Unit, Name = #Name WHERE (ModelID = #ModelID)">
<DeleteParameters>
<asp:Parameter Name="ModelID"></asp:Parameter>
</DeleteParameters>
I am currently trying to fill out a table in asp.net and sql. The table needs to fill out according to the Client ID but i am unsure how to do this.
Here is the code with a hard coded value that is drawing into the table. This works. I want to replace the '1004258' with the ClientIDTxt textbox value.
Any ideas and help please?
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:RCADSCONNECTION %>"
SelectCommand="SELECT CN.ClientID, CN.GivenName1, CN.Surname, CI.DateOfBirth FROM [RioOds].dbo.ClientIndex CI
LEFT JOIN [RioOds].[dbo].[ClientName] CN ON CN.ClientID = CI.ClientID
AND CN.AliasType = '1'
AND CN.EndDate IS NULL
WHERE CN.ClientID = 1004258;"></asp:SqlDataSource>
You want to use paramaters so you would want it to look something like this
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:RCADSCONNECTION %>"
SelectCommand="SELECT CN.ClientID, CN.GivenName1, CN.Surname, CI.DateOfBirth FROM [RioOds].dbo.ClientIndex CI
LEFT JOIN [RioOds].[dbo].[ClientName] CN ON CN.ClientID = CI.ClientID
AND CN.AliasType = '1'
AND CN.EndDate IS NULL
WHERE CN.ClientID = #ClientID;"><SelectParameters>
<asp:ControlParameter Name="ClientID" ControlID="ClientIDTxt" PropertyName="Text" />
</SelectParameters>
You can use the SqlDataSource2.SelectParameters property. There you can map the id of the text box and the property(textBox1.Text) of the textBox you want to map to.
Refer to this link.
Hope it helps.
I have a page that list products from table based on values passed in querystring.
ex:- abc/product.aspx/subcat=Mobile&bnd=Samsung
Here it will display all mobile with brand Samsung
How can i display all mobile irrespective of the brand if bnd is empty or not passed i.e only subcat value is passed.
I need SqlDataSource command to do the same. My current query is as shown below:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:shoppingConnectionString2 %>"
SelectCommand="SELECT * FROM [ProductDetails] WHERE (([Sub_category] = #Sub_category) AND ([Brand] = #Brand OR #Brand IS NULL))"
onselecting="SqlDataSource1_Selecting">
<SelectParameters>
<asp:QueryStringParameter Name="Sub_category" QueryStringField="subcat"
Type="String" DefaultValue="" "" />
<asp:QueryStringParameter Name="Brand" QueryStringField="bnd" Type="String"
DefaultValue="IS NULL" ConvertEmptyStringToNull="True" />
</SelectParameters>
</asp:SqlDataSource>
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string subcat = Request.QueryString["subcat"];
string bnd = Request.QueryString["bnd"];
string query = "SELECT * FROM [ProductDetails] WHERE ([Sub_category] = " + subcat + ")";
if (!String.IsNullOrEmpty(bnd))
{
query += " AND ([Brand] = " + bnd + ")";
}
SqlDataSource1.SelectCommand = query;
}
}
HTML markup:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:shoppingConnectionString2 %>"
SelectCommand="SELECT * FROM [ProductDetails]"
onselecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
(Note the removed SelectParameters)
I've never used a SqlDataSource before, but this is similar to what I'd do for an ObjectDataSource. Would the above code work for your scenario?
EDIT : Please note that this method is open to SQL injection attacks, so you ought to validate/sanitize the querystring parameters first.
I have a drop down list that pulls data from a database to show some of its options:
<asp:DropDownList ID="DropDownList3" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Name" DataValueField="PK_Task" AppendDataBoundItems="true" CssClass="dropDownList">
<asp:ListItem Selected="True">General Support</asp:ListItem>
<asp:ListItem>Vacation</asp:ListItem>
<asp:ListItem>Sick Leave</asp:ListItem>
<asp:ListItem>Something Else</asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
SelectCommand="SELECT [PK_Task], [Name] FROM [Task] WHERE ([PointPerson] LIKE '%' + #PointPerson + '%') AND [Status] NOT LIKE 'Done'">
<SelectParameters>
<asp:QueryStringParameter Name="PointPerson" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
As you can see, I only display the [Name] column:
DataTextField="Name"
But I am also getting the primary key SELECT [PK_Task], [Name] FROM:
DataValueField="PK_Task"
How do I get access to this primary key based on user selection. If a user selects a specific 'name' from the drop down menu, what C# can I use to get the corresponding primary key returned to me so I can save it in a variable?
The closest I have gotten is this, but all it does is return the string "PK_Task":
String taskID = DropDownList3.DataValueField;
You can use the SelectedValue property:
// to set
DropDownList3.SelectedValue = "1";
// to read
string value = DropDownList3.SelectedValue;
try this
String taskID = DropDownList3.SelectedItem.Value;
any way you can use
either
dropdown3.selectedvalue
or
dropdown3.selecteditem.value