How to set parameters for SqlDataSource UpdateCommand - asp.net

For a Gridview:
I am trying to use a stored procedure for the first time in a SQLDataSource for the UpdateCommand:
<asp:SqlDataSource ID="TECT_DataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:OracleConnectionString %>"
ProviderName="<%$ ConnectionStrings:OracleConnectionString.ProviderName %>"
SelectCommand="SELECT MPID, User_Id, Last_Name, First_Name
FROM Scripts.vw_Tect_Exam"
UpdateCommand="P_TECT_UPD_EXAM_ID" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="MPID" Type="Int32" />
<asp:Parameter Name="User_Id" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
I am wondering how the UpdateParameters get their values set, since I only specify a name?
The procedure P_TECT_UPD_EXAM_ID expects two parameters as input: "in_MPID" and "in_UserId"
I am also wondering how to map those values to the input parameters for the procedure as the names are different?

You can set them something like this :
Example : In the example MPID is the sql parameter name #MPID
<UpdateParameters>
<asp:ControlParameter Name="MPID" ControlID="MPID_TextBox" PropertyName="Text />
<asp:ControlParameter Name="User_Id" ControlID="User_Id_TextBox" PropertyName="Text />
</UpdateParameters>
Correction: Just spotted your proc param names so it must be
<asp:ControlParameter Name="in_MPID" ...............
<asp:ControlParameter Name="in_User_Id" ...............
Hope this helps....

I really wouldn't use a SqlDataSource. It will be much easier if you make the call to the database in the code-behind (or a better yet in a Data Access Layer).
If you use a SqlDataSource the stored procedure call will only be available on that page. Every time you want to make that same call you will have to copy and paste the SqlDataSource or make a UserControl out of it.
The following example uses the Entity Framework to connect to the database and retrieve records:
public List<Record> GetAllRecordsByUserName(string credentials)
{
List<Record> recordList;
using (CustomEntities context = new CustomEntities())
{
IQueryable<Section> recordQuery = from records in context.Records
where records.UserName == credentials
select records;
recordList = recordQuery.ToList<Record>();
}
return recordList;
}

Related

Error: Cannot insert the value NULL into column

I have a gridview tied to a sqldatasource. Trying to run the update stored proc.
I keep getting this error...
Cannot insert the value NULL into column 'MyFKId', table 'dbo.MyTable';
column does not allow nulls. INSERT fails. The statement has been
terminated.
I know the answer seems obvious, that I am trying to insert a null into a column that doesn't allow it. The problem is that I know I am putting a value into 'MyFKId'. In my code I set a breakpoint and saw there was a value after this line ran...
dsMyDataSource.UpdateParameters.Item("MyFKId").DefaultValue = SomeId
SomeId did indeed have a value. It was not null. What do you think could be wrong? The SQLDataSource is setup as follows...
<asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
SelectCommand="xxxxx" SelectCommandType="StoredProcedure"
UpdateCommandType="Text"
UpdateCommand=" Execute s_MyStoredProc #MyFKId,...#UserId">
<UpdateParameters>
<asp:Parameter Name="MyFKId" Type="Int32" />
...
</UpdateParameters>
</asp:SqlDataSource>
Don't use EXECUTE to call your stored procedure. Use InsertCommand
<asp:SqlDataSource ID="dsMyDataSource" runat="server" ConnectionString="xxxxx"
InsertCommand="s_MyStoredProc" InsertCommandType="StoredProcedure">
<InsertParameters>
<asp:Parameter Name="MyFKId" Type="Int32" />
...
</InsertParameters>
</asp:SqlDataSource>
And use InsertParameters to set the parameter.
dsMyDataSource.InsertParameters("MyFKId").DefaultValue = SomeId
Can not try it but hope that will solve it for you.

ASP Gridview Select statement not working (GUID)

I'm using ASP.net to use a gridview. The gridview's sql data source is a select statement where ID = a value (a GUID) from a querystring.
However, when I try and preview it, it doesn't show any results. If I change it to a different type (int) of 1, then it works fine and shows results.
If I do the select statement in SQL Server Management Studio (with the GUID), it works fine and shows the results.
Here's the code for the data:
<asp:SqlDataSource ID="SQLDataSourceL" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString1 %>"
SelectCommand="SELECT [FName], [Date] FROM [Table1] WHERE (([FName] = #FName) AND ([Type] = #Type)) ORDER BY [Date] DESC, [FName]">
<SelectParameters>
<asp:QueryStringParameter Name="FName" QueryStringField="id" Type="Object" />
<asp:Parameter DefaultValue="X" Name="Type" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
(I've changed the column names).
Hope you can help.
Try changing the QueryStringParameter to the suitable type, eg:
<asp:QueryStringParameter Name="FName" QueryStringField="id" Type="String" />

Control doesn't bind to SqlDataSource on page load with blank parameters

Suppose you have a SQLDataSource that looks like this:
<asp:SqlDataSource ID="sqldsSample" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
SelectCommand="SELECT [col1], [col2] FROM [tbl] WHERE [col3] = #val) ORDER BY [col1] DESC;">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="val" Type="Boolean" />
<asp:Parameter DefaultValue="" Name="val2" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Just assume you have decided you want to declare the 'val2' parameter here and you don't want to bother with adding and/or removing parameters later in the code-behind (say, to change the SelectCommand to do some filtering with some extra criteria).
It will fail without an error - the control will just show up empty.
You have to specify a default value for the parameter even if it's not used. For example, just putting in a space character will work:
<asp:Parameter DefaultValue=" " Name="val2" Type="String" />
Note that it still won't work if you omit the DefaultValue property.
There is also the CancelSelectOnNullParameter parameter on SqlDataSource - you need to set this to false if you expect any of your parameters to be null.
It's not very obvious and it has caught me out a few times!

SqlDataSource SelectCommand using 2 Parameters and LIKE does not work

I have the following selectcommand in a SqlDataSource, my problem is that it only returns data when I have values for both Parameters. I need it to also return data when only 1 has a value.
SelectCommand="SELECT user.UserId, UserProfiles.Company, UserProfiles.Address, UserProfiles.JV2, UserProfiles.DB1, aspnet_Membership.Email, user.UserName
FROM user INNER JOIN UserProfiles ON user.UserId = UserProfiles.UserId WHERE (#UserName IS NULL OR #UserName = '' OR user.UserName LIKE '%'+#UserName+'%' ) AND
(#Email IS NULL OR #Email = '' OR user.Email LIKE '%'+#Email+'%')
with the following Parameters
<SelectParameters>
<asp:ControlParameter Name="Email" ControlID="eMail" PropertyName="Text" Type="String" />
<asp:ControlParameter Name="UserName" ControlID="userName" PropertyName="Text" Type="String" />
When I run the query in SQL Server Management Studio and it returns data when I enter only 1 value or both.
When I run the form I only get data returned when I enter values in to both fields.
I found this post: SqlDataSource SelectCommand using LIKE does not work which is similar but the solution hasn't helped.
Obviously I've done something wrong, I just can't see what.
I have had a similar issue and adding this to the sqldatasource fixed it:
<asp:SqlDataSource CancelSelectOnNullParameter="false">

execute stored procedure using sqldatasource and get return value in vb.net

How can I execute a stored procedure using sqldatasource and get the return value in vb.net.
Thanks,
Terri
The method you are looking for is DataBind. Call it using mySqlDataSource.DataBind()
<asp:SqlDataSource
ID="sds2"
runat="server"
ConnectionString="..."
SelectCommand="spTest"
SelectCommandType="StoredProcedure"
>
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" PropertyName="Text"
Name="ParamName" Type="Int32" DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gv" runat="server" DataSourceID="sds2"></asp:GridView>
The stored procedure is executed when you call DataBind. The DataBind method is called automatically if the DataSourceID property of the GridView control refers to a valid data source control.
You need to use a SqlConnection with a SqlCommand, like this:
using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand("StoredProcedureName", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.AddWithValue("SomeParam", someValue);
object result = command.ExecuteScalar();
}
If you already have the SP returning a value then you have to grab the value in the corresponding event for the data source. AKA - Inserted, Selected, etc...
Here's a couple links illustrating the point.
http://fredrik.nsquared2.com/viewpost.aspx?PostID=162
http://www.velocityreviews.com/forums/t86158-re-how-to-retrieve-an-output-parameter-using-sqldatasource-control.html
<asp:SqlDataSource ID="ADSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ADConnection %>"
SelectCommand="GetProfile" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter ControlID="InputTextBox" Name="Host" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
GetProfile is the stored proc name and Host is parameter name, which is retreived from a texbox called InputTextBox

Resources