whats the diffrence btw this two SqlDataSource1?
<SelectParameters>
<asp:Parameter Name="user" />
</SelectParameters>
SqlDataSource1.SelectParameters("user").DefaultValue = "some value";
SqlDataSource1.SelectParameters("#param",user);
When i use
SqlDataSource1.SelectParameters("user").DefaultValue = "some value";
it works but
SqlDataSource1.SelectParameters("#param",user); doesn't:
In the selecting event of the SqlDataSource control pass the value to the to the parameter:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters["#user"].Value="value";
}
I think you should use smth. like this:
SqlDataSource1.SelectParameters.Add("#param",user);
that has some overloads.
Related
I have an asp.net program.
Here I want to keep Insert Facility of the Details view. But in the interface I want to keep the 'UserName' attribute of the new Record as fixed and same as the UserName of the Logged in user. I also want to check it on Server side whether it corresponds to the logged in user's username.
How can I achieve this functionality.
I have made the BoundField ReadOnly. But I can't find anything else suitable for this functionality.
In your .aspx do the following:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" OnInserting="SqlDataSource1_Inserting">
<InsertParameters>
<asp:Parameter Type="String" Name="Username" />
//Place other parameters here
</InsertParameters>
</asp:SqlDataSource>
In your code behind:
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
SqlDataSource1.InsertParameters["Username"].DefaultValue = Context.User.Identity.Name;
}
I'm passing a value thru a querystring, I want that value to be part of my selectcommand to build my GridView. But I'm getting the following error. Any ideas how to fix?
Error: Conversion failed when converting the nvarchar value '<%=QueryString2%>' to data type int.
C#:
protected void Page_Load(object sender, EventArgs e)
{
QueryString=Convert.ToInt32(Request.QueryString["tourid"]);
}
private int _querystring;
public int QueryString
{
get
{
return _querystring;
}
set
{
_querystring = value;
}
}
ASPX:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChinatowndbConnString %>"
SelectCommand="SELECT * FROM [vwSchedule] Where TourId=#tid">
<SelectParameters>
<asp:Parameter DefaultValue="<%=QueryString%>" Name="tid" />
</SelectParameters>
</asp:SqlDataSource>
You can't use <%= ... %> to assign the value of a server-control property. As a result, your DefaultValue is being set to the literal string "<%=QueryString%>", which is not a valid integer.
Try using a QueryStringParameter instead:
<SelectParameters>
<asp:QueryStringParameter
Name="tid"
DbType="Int32"
QueryStringField="tourid"
/>
</SelectParameters>
I use below code :
.aspx
<asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ConnectionStrings:Con %>" runat="server"
SelectCommand="select * from DorePayvar" OnSelecting="selecting"
UpdateCommand="update DorePayvar set Name=#name" OnUpdating="SqlDataSource1_Updating">
<UpdateParameters>
<asp:Parameter Type="String" Name="name" DefaultValue="Sajjad" />
</UpdateParameters>
</asp:SqlDataSource>
.cs
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["name"].Value = "bvhjbjh";
}
but when i press update button occur below error
An SqlParameter with ParameterName 'name' is not contained by this
SqlParameterCollection.
You are missing the # symbol.
The following should work :
protected void SqlDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#name"].Value = "bvhjbjh";
}
For more information on using parameters with sqldatasource check this out.
The problem could be that there is not a parameter in your parameter list with the name "name".
Try:
e.Command.Parameters.Add(new SqlParameter("name", "bvhjbjh"));
A user control contains a ListView, ObjectDataSource and a CustomerID property, is there a way to pass CustomerID to ObjectDataSource.
it seems ControlParameter does not fix the problem.
Regards
ControlParameter must be used for to get a parameter from a Control. To get a parameter from a property you have to get it from code behind:
<asp:ObjectDataSource OnSelecting="OdsOnSelecting" .... >
<SelectParameters>
<asp:Parameter Name="CustomerID" />
</SelectParameters>
</asp:objectDataSource>
protected void OdsOnSelecting(object sender, ObjectDataSourceMethodEventArgs e) {
e.InputParameters["CustomerID"] = CustemerID;
}
Respond to this event:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.objectcreating.aspx
Try creating an attribute/property for the usercontrol in question, THEN do what is mentioned in the two post before this one...
I have a dropdownlist which is populated with data from an SQL db. This is what I might have in the aspx file. How do I move (as much as possible) the code from the aspx file to the aspx.cs file to implement the code behind technique?
I mean at least the SELECT portion.
Thanks.
<asp:DropDownList ID="DropDownList1" ... runat="server"/>
...
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:Pubs %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = #state">
<SelectParameters>
<asp:ControlParameter Name="state" ControlID="DropDownList1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
suppose you are binding a grid with data source SqlDataSource1 then you can catch SelectedIndexChanged event in codebehind and get data to bind the grid like this:
ASPX file:
<asp:DropDownList ID="DropDownList1" runat="server"
OnSelectedIndexChanged="ddlChanged" />
C# (codebehind):
protected void ddlChanged(object sender, EventArgs e)
{
var cs=..;//get connection string
using(var con=new SqlConnection(cs))
{
using(var com=new SqlCommand(con))
{
com.Open();
com.CommandType = CommandType.Text;
com.CommandText="SELECT [au_id], [au_lname], [au_fname], [state]
FROM [authors] WHERE [state] = #state";
var state=....;//GET VALUE OF STATE FROM DROPDOWN
var p = com.Parameters.Add("#state");//set other properties
p.Value = state;
using(var adptr=new SqlDataAdapter(com))
{
var dtb=new DataTable();
adptr.Fill(dtb);
grid.DataSource=dtb;
grid.DataBind();
}
}
}
}
The SelectCommand is a property of the datasource object you are using. These can be applied in the code behind as needed, but you may want to do it in an overridden Init page function as this might be used quite early on in the asp.net page life cycle. for eg, although I'm not sure exactly where.
protected override OnInit(object sender, EventArgs e)
{
dsMySource.SelectCommand = "SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors] WHERE [state] = #state"
}
You are also going to have to make sure the #state parameter is used correctly in the code behind as well, this can be accessed also as a property (dsMySource.SelectParameters).