.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Post New Web Links

can you use if else in sqldatasource if else parameters?

Posted By:      Posted Date: October 03, 2010    Points: 0   Category :ASP.Net
 

I am trying to check whether a username exists in my database before inserting the new row & display a message to the user if it does.

On my SqlDataSource my InsertCommand looks like this:

InsertCommand="IF NOT EXISTS (SELECT * FROM Users WHERE UserName = @UserName) BEGIN INSERT [Users] ([UserName], [FirstName], [LastName], [City], [State], [Country], [CompanyName], [Active], [PasswordHash]) VALUES (@UserName, @FirstName, @LastName, @City, @State, @Country, @CompanyName, @Active, @PasswordHash) END"

 

This works as expected: If the username is unique, the row is created.

If the username already exists, the row is not added.

 

The problem is, if the row already exists, the insert is rejected but the user is not notified.

So my question is, is there a way I can notify the user?

Maybe something like: InsertCommand = "If Not Exists (select....) Begin Insert... Else Notify User Somehow End

 

This is a basic database, I'm not using the built-in membership provider nor will be.

 

Thanks!

 




View Complete Post


More Related Resource Links

SqlDataSource and Parameters query

  

Hi everyone,

I'm on the edge wondering is it possible ? I have got 2 questions. Please help me out.

1. I have a GridView on my page and it uses sqldatasource with parameterized query. What I want to do is, on page load (where nothing has been selected so no parameter supplied), I want it to query everything (something like SELECT * FROM [this_table]) but since my SelectCommand is something like

SELECT * FROM [this_table] WHERE [this_column] = @someParameters AND [that_column] = @someParameters.

Can I play around with default value to achieve something like that but how ? Now, when the page loads, it doesn't show anything (No Gridview).

2. On my page, I made something like (username, gender, address, and more) and one single search button. That means, no single control enable auto postback. What I am trying to accomplish is building dynamic query

(if username specifed -> SELECT * FROM [this_table] WHERE [username] LIKE @username).

If both username and gender are specified (SELECT * FROM [this_table] WHERE [username] LIKE @username AND [gender] = @gender) and you know the rest. How can I do this using GridView and SqlDataSource ? To my knowledge, I can only specify one SELECT statement in a sqldatasource.  I am stucked and desparately looking for help. Thanks all.


Mapping SqlDataSource Parameters to Formview with Multiple DataKeys

  
Hi I have:a FormView that is linked to SqlDataSource1a Repeater control (inside the formview) that is linked to SqlDataSource2Thus representing a simple "1 to many" or "parent/child" relationship in the database!SqlDataSource1 is simple in that it just retrieves a straight set of records from a table and they are displayed back in the FormView (nothing special happening there). SqlDataSource2 has parameters that  filter the contents of the repeater based on the record that is shown in the Formview. This would normally be simple enough to set up by setting the parameter to the formview.selectedvalue property.HOWEVER - THE PROBLEM ARISES...The formview has multiple DataKeyNames because the table that the SqlDataSource1 links to has a compound key. So to filter the Repeater correctly SqlDataSource2 needs to map 3 parameters to 3 data key values in the formview.Any ideas how to do this?I've tried setting the SqlDataSource2 parameters to:<SelectParameters>   <asp:ControlParameter ControlID="FormView1" Name="Parameter1" PropertyName="DataKey(0)" />   <asp:ControlParameter ControlID="FormView1" Name="Parameter2" PropertyName="DataKey(1)" />   <asp:ControlParameter ControlID="FormView1" Name="Parameter3" PropertyName="DataK

Mapping SqlDataSource Parameters to Formview with multiple DataKeys

  
Hi I have:a FormView that is linked to SqlDataSource1a Repeater control (inside the formview) that is linked to SqlDataSource2Thus representing a simple "1 to many" or "parent/child" relationship in the database!SqlDataSource1 is simple in that it just retrieves a straight set of records from a table and they are displayed back in the FormView (nothing special happening there). SqlDataSource2 has parameters that  filter the contents of the repeater based on the record that is shown in the Formview. This would normally be simple enough to set up by setting the parameter to the formview.selectedvalue property.HOWEVER - THE PROBLEM ARISES...The formview has multiple DataKeyNames because the table that the SqlDataSource1 links to has a compound key. So to filter the Repeater correctly SqlDataSource2 needs to map 3 parameters to 3 data key values in the formview.Any ideas how to do this?I've tried setting the SqlDataSource2 parameters to:<SelectParameters>   <asp:ControlParameter ControlID="FormView1" Name="Parameter1" PropertyName="DataKey(0)" />   <asp:ControlParameter ControlID="FormView1" Name="Parameter2" PropertyName="DataKey(1)" />   <asp:ControlParameter ControlID="FormView1" Name="Parameter3" PropertyName="DataK

SqlDataSource with a lot of parameters

  
Hi everyone,I have asked similiar question before where I have two control on the page header says "username" and "gender". User can select both the control or either one and the page will execute searching based on the criteria supplied. So if username suppliedSELECT * FROM [this_table] WHERE username = @username and if gender suppliedSELECT * FROM [this_table] WHERE gender = @gender and if both supplied SELECT * FROM [this_table] WHERE username = @username AND gender = @genderI know this sounds not very complicated but in my case, I am having like more than 10 controls up there. It is something like username, gender, age, date of brith, title, father name, frst name, last name, blur blur blur and it's a lot then I started to lost. if i were to use if else caluse on code-behind and produce query, it would end up writing a lot of if-else and if i use stored procedure (with if-else) then my stored procedure would contains a lot of if-else statement. I am fairly new to this concept and if I'm on the wrong track, please correct me. Is there any better way to save my hair without actually going through each if-else statement to build the query ? Thanks all.p.s. I'm using GridView and SqlDataSource command to display the result back on .aspx page.

SqlDataSource : in what order do parameters get fed to stored proc?

  
Hello, I'm wondering if someone can explain how a SqlDataSource determines the order in which parameters are fed to a stored procedure. I'm not able to find clear documentation.So suppose a proc has: CREATE PROCEDURE  updateOneCar@CarActiveFlagCheck int, @CommentsForCar varchar(1500) = NULL, @manufacturerID int...and then suppose the ASP.NET page has the manufacturerID supplied by a querystring, and has a FormView as follows: <asp:FormView ID="FormView1" runat="server" DataSourceID="SQL01"          DataKeyNames="manufacturerID" EditRowStyle-Wrap="False" RenderOuterTable="False">         <EditItemTemplate>         <div>             manufacturerID: <%# Eval("manufacturerID")%>             <br />             Is my Car on?: <%# Eval("carFlag")%>             <br />             Is my car Active Flag:             <asp:CheckBox ID="CarActiveFlagCheckBox" runat="server"              

sqldatasource parameters from code

  

Hi I'm trying to pass default value to a sql parameter which is

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
    SelectCommand="SELECT * FROM [Activ] WHERE ([DayNum] = @DayNum)">
    <SelectParameters>
        <asp:Parameter Name="DayNum" Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>
and the Page_Load goes like this
Int32  MyDay;
            MyDay = (Int32)DateTime.Today.DayOfWeek;
            MyDay = MyDay + 1;
            SqlDataSource1.SelectParameters["DayNum"].DefaultValue = MyDay;
Intellisense says that I can´t convert int into string underlining red MyDay, I get it but, if I use ToString
then the SQL statement won't work since DayNum is int.
 
How can I work this out? Thank you


 


All parameters null in Update of SQLDataSource?

  

Hi All,

I have a FormView which is bound to an SQLDataSource. When a user clicks on a save button the form, I need to write any changes made to the FormView back to the table.

I have a Stored Procedure on my MSSQL 2005 database that should be executed when the user clicks on the save button.

It appears the Update is firing, but when I trap the DbCommand object in the SQLDataSource's 'Updating' event, all of the parameters that should be passed to the Stored Procedure are coming through as NULLs. Consequently, the Stored Procedure isn't updating anything.

In the click event of the save button I'm explicitly firing the SQLDataSource's Update method:

this.SqlDataSource4.Update();


The parameters in the SQLDataSource <UpdateParameters></UpdateParameters> section appear to be correct.

Can anyone give me an idea on what to investigate to figure out why it appears the Update is only picking up NULLs?

Many thanks in adavnce,

pt


Update Parameters with SQLDataSource and VB code behind

  

I have been going around the houses for several days now. I have an sql datasource that uses a stored Proc  to uodate.  I set the updateParameters values from vbcode behind

        Me.SqlAdministration.UpdateParameters("Licence").DefaultValue = Me.TxtLicence.ToString
        Me.SqlAdministration.UpdateParameters("CompanyName").DefaultValue = CType(FormView1.FindControl("CompanyNameTextBox"), TextBox).Text
        Me.SqlAdministration.UpdateParameters("Status").DefaultValue = CType(FormView1.FindControl("status"), DropDownList).SelectedValue.ToString
        Me.SqlAdministration.UpdateParameters("EmailAccounts").DefaultValue = CType(FormView1.FindControl("txtEmailAccounts"), TextBox).Text
        Me.SqlAdministration.UpdateParameters("EmailGeneral").DefaultValue = CType(FormView1.FindControl("txtEmailGeneral"), TextBox).Text
        Me.SqlAdministration.UpdateParameters("TelAccounts").DefaultValue = CType(FormView1.FindControl("txtTelAccounts"), TextBox).Text
        Me.SqlAdministration.UpdateParameters("TelGeneral").DefaultValue = CType(FormView1.FindControl("txtTelGeneral"), TextBox).Text
        Me.SqlAdministration.UpdateParameters("CompanyAddress").DefaultValue = CType(FormView1.Find

SqlDataSource WHERE Parameters - QueryString and Default value not working correctly?

  

This is really easy, but I can't seem to get this working correctly...

 

When using SqlDataSource and press on Configure Data Source, I have selected my data source.

I have selected * all records and pressed the WHERE button  (so it starts to bulld a query).

 

 

I have set the following values:

 

Column: NewOrUsed (only contains 1 of 2 values, either "New" or "Used")

Operator: =

Source: QueryString

QueryString Field: (Eval("NewOrUsed").ToString)

Default Value: New

 

 

However it doesn't seem to work, always returns no records even though I know there are a number of records matching "New"

 

 

What am I doing wrong?


Linq: how to share parameters between lambda expressions ?

  
When using Linq to objects, you will quickly feel the need to pass some parameters from a method to another but it's not so easy because each Linq method is not calling the following one. In a Linq sequence, each method is using the result computed by the previous one. So, local contexts are not visible from one method to another.
The compiler is using two technical different ways to let parameters go out of a method.

How to Encrypt Query String Parameters in ASP.NET

  
Encrypt Query String Parameters in ASP.NET.u can send secure data one page another page u can also use query string to encrypt

view state vs request parameters

  

If  the view state is the data entered into the form fields then they are supposed to be available in the request parameters or request body. Then why would we need view state for?


Thanks.



How to retrieve data from SqlDataSource and pass to a variable?

  

Hi there,

I have a page in my website where there is very little code-behind. I only have two lines of code in Page_Load() and the rest of the code is declarative ASP.NET code. I have a FormView control which uses a SqlDataSource to get some order information. The SqlDataSource calls a stored procedure called usp_GetOrderDetails, here's the code for the SqlDataSource:

    <asp:SqlDataSource ID="sdsFormOrderDetails" runat="server" 
ConnectionString="<%$ ConnectionStrings:ForexDB %>"
ProviderName="<%$ ConnectionStrings:ForexDB.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure"
>
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="frmOrderDetails" PropertyName="SelectedValue" />
</UpdateParameters>
</asp:SqlDataSource>

Binding GridView with SqlDataSource at runtime?

  

Hi, I want to create a generic page which contains a gridview and sqldatasource. I want to put these two controls on .aspx (C#) page but bind them at runtime. It should have selectcommand, updatecommand, deletecommand and insertcommand. Stored procedures are created for that. Help is needed to make it. Please post some link or code. Regards, ap.


FormView -- Parameters and asp:Table

  

I've already solved this problem to my satisfaction, I just want to know WHY it works this way.

I have a FormView control bound to an ObjectDataSource.  Let's say our data source is a hypothetical table with three columns:  id, name, age.  Why is it that when I use the <asp:Table> element inside of the FormView, the parameter values are not set, but when I use the standard <table> tag, it works fine?

This seems like a .NET problem to me.  I expect the <asp:Table> element to work in the same way as <table> because they are, for all intents and purposes, the same thing.  Am I missing something?


linqdatasource parameters question

  

Hello,

I'm currently getting to know the linqdatasource control. What would happen if several WHERE parameters are added to the linqdatasource control declaritively in the aspx page but no WHERE clause is defined? 

1. is there any issue with setting up multiple WHERE parameters in the aspx markup for linqdatasource but then deciding which ones actually get used at runtime by setting the actual WHERE clause at run time? Would the unused parameters simply be ignored and cause no issues?

2. when you need to modify WHERE clause or WHERE parameters at runtime do you need to do so in an event handler? such as linqdatasource.selecting?
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linqdatasource.selecting.aspx
I see from the above link it is possible, but is that the standard way to handle controlling filtering in code behind for linqdatasource?

2b. if you use an event such as linqdatasource.selecting to add where parameters and/or modify the where clause in code will that also break the builtin sorting and paging support of the linqdatasource with a gridview? 


SqlDataSource how to change the stored procedures at run time

  

Hi

Is it possible to change the select, insert and update stored procedure of an SqlDataSource at run time. I don't mean to change the definition of the stored procedure, but I mean to change the assignment, i.e. To change the select stored procedure from "selectSP1" to "selectSP2" at run time. Is it possible?

Regards

Dimitris


Categories: 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend