Error in parameter that i use in update command in sqlDataSource - asp.net

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"));

Related

asp.net insertCommand resulting in NULL values

I have two listboxes whose values are sent into a TableC via a button.
<asp:SqlDataSource ID="sql1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionDataBase %>"
SelectCommand="SELECT [Name] + ' (' + [CNbr] + ')' AS FullName, [CNbr] AS CNum FROM [TableA] ORDER BY [Name]">
</asp:SqlDataSource>
<asp:ListBox ID="lst1" runat="server" DataSourceID="sql1" DataTextField="FullName" DataValueField="CNum" AutoPostBack="true">
</asp:ListBox>
<asp:SqlDataSource ID="sql2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionDataBase %>"
InsertCommand="INSERT INTO [TableB] ([CNbr], [RDate]) VALUES (#CNbr, #RDate)"
<InsertParameters>
<asp:Parameter Name="CNbr" Type="String" />
<asp:Parameter Name="RDate" Type="DateTime" />
</InsertParameters>
<asp:ImageButton ID="ibnCheckOutBtn" runat="server"
ImageUrl="./images/vidCheckOutButton.png" onclick="ibnCheckOutBtn_Click" />
ASPX.CS file:
protected void ibnBtn_Click(object sender, ImageClickEventArgs e)
{
sql2.Insert();
}
I don't really know C# and my class doesn't cover most C#, so I am looking for a solution that doesn't use a lot of behind-code. The error I get is:
Cannot insert the value NULL into column 'CNbr', table 'TableB'; column does not allow nulls. INSERT fails. The statement has been terminated.
However, nulls should not occur because the values are taken from a fully populated listbox. Help?
You will need to assign the values to sql2 insert parameters in a similar way as follows:
protected void ibnBtn_Click(object sender, ImageClickEventArgs e)
{
sql2.InsertParameters["CNbr"].DefaultValue = this.FindControl("lst1").SelectedValue;
sql2.InsertParameters["RDate"].DefaultValue = DateTime.Now.ToString();
sql2.Insert();
}

Parameter error when trying to build a selectcommand

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>

how can i access string variable value of .aspx.cs file in .aspx file

i m getting a string value regression as regression = (Session["Regression"]).ToString(); in .aspx.cs file then i want to use this value in .aspx file in SelectCommand of SqlDataSource property as below
SelectCommand="SELECT [issue_oid], [issue_num], [regression],
[status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%#regression%>'"`
.aspx.cs file page_load method is as below
protected void Page_Load(object sender, EventArgs e)
{
if ((Session["UserName"].ToString()) == string.Empty)
{
Server.Transfer("regressionType.aspx");
}
regression = (Session["Regression"]).ToString();
usrname = (Session["UserName"]).ToString();
DataBind();
}
please suggest me how can i do this?
thanks in advance...
You can you Session Variable's value in your SQLDatasource. Like this Example.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:testDatabaseConnectionString %>"
SelectCommand="SELECT * FROM [UserTable] WHERE ([userID] = #UserID)">
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="UserID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
let us know any other concerns on this..
add Session paramter to your sql Datasource like this
<asp:SqlDataSource SelectCommand="SELECT [issue_oid], [issue_num], [regression],
[status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%#regression%>'"`>
<SelectParameters>
<asp:SessionParameter SessionField="Regression" Name="ParameterName" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Try this:
SelectCommand="SELECT [issue_oid], [issue_num], [regression],
[status], [tested_by], [tested_on], [patch_name], [arrived_on], [previous_info], [comment], [is_duplicate] FROM [itt_monthly_patch_issue_list] where status='Not Tested' and `regression='<%=ReturnRegression()%>'"`
In codebehind:
string regression=null;//Before Page_Load
protected string ReturnRegression()
{
//Write logic here to prevent null being returned
return regression;
}
try this:
public StringBuilder regression = new StringBuilder();
regression.Append((Session["Regression"]).ToString());
Then in the .aspx page (in the selct command), you can use "regression" as:
<% =regression %>

Passing Parameters for SqlDataSource from code behind

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.

How to add a "Select All" entry to a Dropdownlist tied to SQLDataSource?

I hvae a dropdownlist tied to a SQLDataSource that returns a list of Categories. Since this is used as part of a search function, how do I add a "Select All" category and ensure that such a selection queries all categories?
Thanks much!!
I sometimes use sql for this:
select value, description from reference_table
union
select -1, 'Select all'
Order by 2
There is also the way where you add the item in the markup, then set the AppendDataBoundItems="true"
Also, see this similar question and answer
Never Use (AppendDataBoundItems="true")!
<asp:DropDownList ID="drpdwnmodel" DataSourceID="Model_Grid" DataTextField="INI" DataValueField="INI" OnSelectedIndexChanged="drprep_SelectedIndexChanged" OnDataBound="DataBoundbyTest_click" AutoPostBack="true" runat="server">
<asp:ListItem Value="All" Text="All"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="Model_Grid" runat="server"
ConnectionString="<%$ ConnectionStrings:Testconnectionstring%>"
SelectCommand="SELECT DISTINCT INI FROM TestTable where [Column] LIKE '%' + #valuename+ '%' ORDER BY INI ASC">
<SelectParameters>
<asp:ControlParameter ControlID="drpdwnmodel" Name="valuename"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
protected void DataBoundbyTest_click(object sender, EventArgs e)
{
drpdwnmodel.Items.Add("All");
drpdwnmodel.Items.Insert(0, new ListItem("- Select -", "0"));
}
protected void drprep_SelectedIndexChanged(object sender, EventArgs e)
{
if (drpdwnmodel.SelectedValue.Equals("0"))
{
GridView1.DataSourceID = "Test_Grid";
}
else {
if (drpdwnmodel.SelectedValue.Equals("All"))
{
GridView1.DataSourceID = "Test_Grid";
}
else
{
GridView1.DataSourceID = "SqlDataSource1";
}
}
}

Resources