With this SqlDataSource, How can i be sure that the data I'm going to update is not modified in the meantime when i fetched data and when i'm about to make the update.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ChartDatabaseConnectionString %>"
SelectCommand="SELECT * FROM [Student]"
UpdateCommand="UPDATE [Student] SET [StudentName] = #StudentName, [DOB] = #DOB, [Age] = #Age, [Course] = #Course, [City] = #City, [MobileNo] =
#MobileNo WHERE [StudentID] = #StudentID">
Is there any GridView method Or Any Parameter I can use ?
There is a propery on SqlDataSource named ConflictDetection. Setting that property to CompareAllValues may do the trick for you.
Related
I want to use a GridView in Visual Studio. I went through the tutorial they have, and I have it set. However I don't see how I can use my Stored Procedure.
It sets me up with this:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="SELECT * FROM [Registrant]"></asp:SqlDataSource>
And I somehow want to replace that SelectCommand with a Stored Procedure, as such:
sqlCmd.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmd.CommandText = "spGetAllRegistrants";
I'm not sure where to put this info of spGetAllRegistrants.
You need to change the SelectCommandType to StoredProcedure:
<asp:SqlDataSource ID="SqlDataSourceViewRegistrants" runat="server"
ConnectionString="<%$ ConnectionStrings:Events2 %>"
SelectCommand="spGetAllRegistrants"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
I would like to use a Session variable created in the code-behind, in an actual tag, primarily within the InsertCommand="" attribute:
Here is my code-behind (VB):
Protected Sub RegSubmitButton_Click(sender As Object, e As EventArgs) Handles RegSubmitButton.Click
For Me.i = 0 To UNPWListBox.Items.Count
If (RegUNTextBox.Text = UNPWListBox.SelectedItem.Text) Then
MsgBox("Username is unavailable. Please choose another username.")
With RegUNTextBox
.Text = ""
.Focus()
End With
Else
username = RegUNTextBox.Text
password = RegPWTextBox.Text
UNPWListBox.Items.Insert(UNPWListBox.SelectedIndex + 1, RegUNTextBox.Text)
'UNPWListBox.Items.Add(username)
UNPWListBox.SelectedItem.Text.Equals(username)
UNPWListBox.SelectedItem.Value.Equals(password)
Session(username) = username
Exit For
End If
Next
Dim newConnection = CreateObject("ADODB.Connection")
Dim createTable = "CREATE TABLE [" + Session(username) + "] (ID int, Artist varchar(50), Title varchar(50), Label varchar(25), PressNumber varchar(15), YearReleased varchar(15), NMValue double, Notes string);"
newConnection.Open("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\allengatorpie\Documents\VBIIT1_DB.accdb")
newConnection.Execute(createTable)
Response.Redirect("AddVinyl.aspx")
Below is the ASP.NET source:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data
Source=C:\Users\allengatorpie\Documents\VBIIT1_DB.accdb"
ProviderName="System.Data.OleDb" SelectCommand="SELECT * FROM
[allengatorpie]" InsertCommand="INSERT INTO [Session(username)] ([Artist], [Title], [Label], [PressNumber], [YearReleased],
[NMValue], [Notes]) VALUES (?, ?, ?, ?, ?, ?, ?)">
The Session(username) variable work perfectly in the code behind, but I need to use it in the ASP control so that SQL knows which table to insert the data into.
Thanks in advance.
You can use inset parameters More details
Example
<asp:sqldatasource
id="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:MyNorthwind %>"
......>
<insertparameters>
<asp:formparameter name="CoName" formfield="CompanyNameBox" />
<asp:formparameter name="Phone" formfield="PhoneBox" />
</insertparameters>
</asp:sqldatasource>
I have an SQL query but it will will only know the movie type in page load. This will be connected to a datalist.
My query is
select * from movies where movieType = #type
Is there a way to pass parameters at runtime to the sql datasource?
Thanks
You will need to add the parameter to your SqlCommand when you make your SQL call.
myCommand.Parameters.AddWithValue("#type", type);
In this example, the variable you are passing in from .Net is 'type'
This MSDN article should help you understand parameter passing.
If you are asking about SqlDataSource control, yes you can use SQL queries and statements that use parameters.
For example:
var source = new SqlDataSource(myConnectionString,
"SELECT * FROM TableName WHERE ColName = #M);
source.SelectParameters.Add("#M", DbType.Int32, ddl.SelectedValue);
MSDN has good article on this topic.
This can be done with SelectParameters in the markup or codebehind
<asp:SqlDataSource
id="SqlDataSource1"
runat="server"
DataSourceMode="DataReader"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
SelectCommand="select * from movies where movieType = #type">
<SelectParameters>
<asp:ControlParameter name="type" ControlID="MyDropdownList" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
More on ControlParameters
I pass TWO StringQueries to new page: /Find.aspx?Color=Blue&Shape=Round
In my Products.aspx I have a GridView with a SelectCommand:
SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo]
FROM [MSD_Store] WHERE ([Color] = ?)"
How do I get the Where clause to handle TWO StringVariables?
Just add necessary part of the SQL query and define another query string parameter:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo] FROM [MSD_Store] WHERE ([Color] = ?) AND ([Shape] = ?)">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="Color" Name="color" />
<asp:QueryStringParameter QueryStringField="Shape" Name="shape" />
</SelectParameters>
</asp:SqlDataSource>
Simply doing
SelectCommand: SelectCommand="SELECT DISTINCT [Name], [Price], [ProductNo] FROM [MSD_Store] WHERE ([Color] = ? AND [Shape = ?])
and then you add another parameter definition, reading your querystring value
Do you mean this
SELECT DISTINCT [Name], [Price], [ProductNo]
FROM [MSD_Store] WHERE ([Color] = ?) AND ([Shape] = ?)
by "two" string variables?
I have an ASP SqlDataSource connected to Sybase DB query, using Select Parameters that are populated by a dropdown:
Dropdown (works OK):
<asp:SqlDataSource ID="dsBondIDList" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [name], [bondID] FROM [bonds]">
</asp:SqlDataSource>
<asp:DropDownList ID="lstBondID" runat="server" DataSourceID="dsBondIDList" DataTextField="name" DataValueField="bondID">
SqlDataSource with parameter:
<asp:SqlDataSource ID="dsBonds" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = #bondID">
<SelectParameters>
<asp:ControlParameter Name="bondID" ControlID="lstBondID" PropertyName="SelectedValue" DefaultValue="-1" />
</SelectParameters>
</asp:SqlDataSource>
But I get an error when I try to run it:
ERROR [HY000] [DataDirect][ODBC Sybase Wire Protocol driver][SQL Server]Must declare variable '#bondID'.
Which of course I would get it I ran the sql literally as displayed. I expect the ASP engine to do the substitution before sending the query (and to use the default value of lstBondID if necessary)
Does anybody know why the #bondID is not being substituted with the lstBondID.SelectedValue?
Thanks in advance
Ryan
I just went through this problem 10 minutes ago and here's the fix:
If you need to keep using the ODBC driver you have, change your query to use ? instead of Named Parameters.
SELECT [ticker], [name], [isin], [currency],
[stock], [maturity], [bid], [ask]
FROM [bonds] where [bondID] = ?
The parameters are used in the order they are added to the parameters collection (This gets hairy when you need to use the parameter more than once in your query etc).
Otherwise you can change drivers. Look for sybdrvodb.dll and regsvr32 it. Then setup your DSN and use that.