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


Top 5 Contributors of the Month
Easy Web
Imran Ghani
Post New Web Links

Update Parameters with SQLDataSource and VB code behind

Posted By:      Posted Date: September 30, 2010    Points: 0   Category :ASP.Net
 

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


View Complete Post


More Related Resource Links

SqlDataSource, FormView, and Update with stored procedure in code behind

  
Hello, Using a FormView with a SqlDataSource, I'm attempting to Update data by calling a stored proc in code behind. I was having trouble getting parameters using Update Parameters in the SqlDataSource, but found a working solution by coding the parameters. The problem now is I'm getting an "Updating is not supported by data source 'XYZ' unless UpdateCommand is specified'. I saw some previous posts on the forums, but didn't find them very enlightening. 

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


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.


Gridview Update outside of SqlDatasource

  

Is it possible to use a SQLDataSource to populate a gridview but not use the SQLDataSource for Updating?

I wanted to add the update code in the Gridview_RowUpdating event since it is a complex update I need to do.


I get this message when i click the update button


Updating is not supported by data source 'SqlDataSource1' unless UpdateCommand is specified.


Error code 737D-Trying to download update

  
My computer is recommending that I download this: Security Update for SQL Server 2005 Service Pack 3 (KB970892) But it tells me this: Installation status: Failed Error details: Code 737D Update type: Important A security issue has been identified in the SQL Server 2005 Service Pack 3 that could allow an attacker to compromise your system and gain control over it. You can help protect your computer by installing this update from Microsoft. After you install this item, you may have to restart your computer. System: Windows Vista Home Premium Service Pack 2 HP Pavilion dv6700 notebook PC 32-bit Operating System Any ideas???  

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 - Update Parameter with javascript

  
Hello, It's possible change the DefaultValue of Parameter with javascript?   Tks

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"              

DetailsView error "must be in edit mode to update a record" when I have set edit mode in code behind

  
Hi,I have a DetailsView within an UpdatePanel.  I programmatically put the DetailsView into edit mode, change the fields then call the UpdateItem method which causes the "must be in edit mode to update a record" exception.  Am I missing something blindingly obvious? <asp:UpdatePanel ID="upSpecificShout" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true" > <ContentTemplate> <asp:DetailsView ID="dvSingleShout" DataSourceID="sdsSingleShout" runat="server" AutoGenerateRows="false" OnDataBound="SetDVIcons" HeaderStyle-CssClass="noDisplay" DefaultMode="ReadOnly" AutoGenerateEditButton="true" OnItemCommand="dvItemCommand"> <fields> <asp:TemplateField ShowHeader="false"> <ItemTemplate> <asp:Panel ID="dlSpecificDetailsViewPanel" runat="server" CssClass="i_am_a_shout"> <h2><asp:LinkButton ID="lblShoutTitle" runat="server" Text='<%# Bind("Shout_Title") %>' On

Fileupload with database parameters that need to return nothing on update

  
Hi All, Got a problem with regards to the file upload control on database updates. The problem is in my CMS, when a user tries to update content for a page it will add null to the fields containing the image path. What I need is for the system to check if the uploader has no image and if hasn't it has to do nothing. It can't add empty strings or null values as this will remove the original image path. Although they still need to be able to do image updates if the uploader has a file(this bits not a problem). Heres a snipet of code to demonstrate what I'm trying to achieve. protected void Button1_Click(object sender, EventArgs e) { if (Page.IsPostBack) { AddMenu menu = new AddMenu(); string APP_PATH = Server.MapPath(Common.GetAppSetting("_MENU_PATH")); if (FileUpload1.HasFile == false) { menu.Menu_Image1 = /////// Its this bit that has to do nothing //// Menu_Image1 calls a Parameter on the data layer /// Ie cmd.Parameters.AddwithValue("@image", _menu_image1) } else if (FileUpload1.HasFile) { if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.ContentLength > 0) { Uploa

Input parameters from Workflow No-Code Or Xaml Only

  
I have many activities and a customized workflow Only Xaml and I can not pass a value to a property of a custom activity, following the example of Workflow1.xoml   <pre lang="x-xml"><SequentialWorkflowActivity x:Name="Workflow1" xmlns:ns0="clr-namespace:CustomActivities;Assembly=CustomActivities, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/workflow"> <ns0:ReadLineActivity x:Name="readLineActivity1" InputText="" /> <ns0:WriteLineActivity x:Name="writeLineActivity1" OutputText="{ActivityBind readLineActivity1,Path=InputText}" /> </SequentialWorkflowActivity> based on this workflow I pass a value for the property InputText activity readLineActivity1   Thanks a lot!

How to add Select Control parameters in Code Behind

  

I have a need to add my page controls dynamically in code behind.

I do well, adding a dropdownlist, a SQL datasource to populate it, setting it to autopostback=true, and adding a gridview and its SQL datasource.

What I'm unsure about is how to programmatically refer to the SelectParameter, being a control (the DropDownList), and its selected item.

any ideas? 


Where to place code to update database using WPF in VS2010?

  

I have an app in WPF that has numerous controls that are bound to datatables that are filled from sqlserver.

I now need to add the code for updates, inserts and deletes

Where should I put the code to implement this?

I found code

this

._adapter.UpdateCommand.CommandText = "tool generated update command


in an auto-generated section of the source file that VS created it looks almost correct but I have never before seen an update statement with an order by clause.
I also found multiple instances of:
  if ((this .Adapter.UpdateCommand != null )) {
 this .Adapter.UpdateCommand.Connection = value ;             }
But I cannot find where value is defined or assigned
I have a tabbed interface with a selection list to move back and forth through the datatable. I want to update the data base with all changes to the current selection when a user changes selection or selects a different tab So what is the preferred method to initiate the update and where should

can you use if else in sqldatasource if else parameters?

  

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!

 


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