Create Grid view with master page in asp - c#

i am new in asp, i create a grid view with master page. but it show an error in connection string. can anyone help me please?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
AutoGenerateEditButton="true" AutoGenerateDeleteButton="false"
DataSourceID="SqlDataSource1" Font-Names="Arial" Font-Size="Smaller" DataKeyNames="Logid" >
<columns>
<asp:BoundField DataField="Logid" HeaderText="Logid" SortExpression="Logid" />
<asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username" />
<asp:BoundField DataField="PASSWORD" HeaderText="PASSWORD" SortExpression="PASSWORD" />
<asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
<asp:BoundField DataField="E_MAIL" HeaderText="E_MAIL" SortExpression="E_MAIL" />
</columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1"runat="server" ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL]"
UpdateCommand="Update Log_Users SET Logid=#Logid, Username=#Username, PASSWORD=#PASSWORD, Status=#Status, E_mail=#E_mail"
DeleteCommand="DELETE FROM Log_Users WHERE Logid = #Logid">
<UpdateParameters>
<asp:Parameter Name="Logid" />
<asp:Parameter Name="Username" />
<asp:Parameter Name="PASSWORD" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="E_MAIL" />
</UpdateParameters>
</asp:SqlDataSource>
</asp:Content>

Are you sure it's not something simple like the space between ID="SqlDataSource1" and runat="server" bit.
This would cause the error "Server Tag is not well formed" error.

Connection string name should be match with what you have given in web config so,
change ConnectionString="<%$ ConnectionString:SaqibConnectionString %>" to
ConnectionString="<%$ ConnectionStrings:db %>"
or change the name in configuration file which given to your connection string as SaqibConnectionString
Update:
ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
/\
you missed `s` here
You made mistake on Connection String setting, it should be ConnectionStrings, note that s at the end
try below
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL] from Log_Users"
UpdateCommand="Update Log_Users SET Logid=#Logid, Username=#Username, PASSWORD=#PASSWORD, Status=#Status, E_mail=#E_mail"
DeleteCommand="DELETE FROM Log_Users WHERE Logid = #Logid"
ConnectionString="<%$ ConnectionStrings:db %>">
<DeleteParameters>
<asp:Parameter Name="Logid" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="Logid" />
<asp:Parameter Name="Username" />
<asp:Parameter Name="PASSWORD" />
<asp:Parameter Name="Status" />
<asp:Parameter Name="E_MAIL" />
</UpdateParameters>
</asp:SqlDataSource>

As seen above your connectionstring name is db but you are using SaqibConnectionString as connection string.so use ConnectionString="<%$ ConnectionString:db %>" in place of
ConnectionString="<%$ ConnectionString:SaqibConnectionString %>"
Update1
and your select statement is not right
SelectCommand="SELECT [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL]"
select statement should be
select [Logid], [Username], [PASSWORD], [Time_Logged_in], [Time_Logged_Out], [Status], [Date_Logged_in], [E_MAIL] from Log_Users you are missing from tablename in your select statement
Update2
you do not have space between ID="SqlDataSource1" and runat="server"

Related

Can I use the result of an sqldatasource (a simple string) to use it as a parameter of another sqldatasource?

<asp:SqlDataSource ID="UsersUsernameSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:UserQueries %>"
ProviderName="<%$ ConnectionStrings:UserQueries.ProviderName %>"
SelectCommand="SELECT "FIRSTNAME" FROM "USERS" WHERE ("USERNAME" = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="UsersNameLabel" Name="USERNAME" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
This is my code. My query is that I want to select a name where the username equals with the result of the other SqlDataSource (this one works fine, I checked it)
So can I set the result of the datasource as a parameter? And if yes, then how?

Parameterize From Statement in SqlDataSource C# asp.net

I am trying to Parameterize the from statement of an asp:SqlDatasource Select Command. My current command is hard coded:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from WIN_Dev.INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'Procedure'">
</asp:SqlDataSource>
However I am looking for something more like this:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from #Path where ROUTINE_TYPE = 'Procedure'">
<SelectParameters>
<asp:ControlParameter ControlID="lblDB" PropertyName="text" Name="Path" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Or even more preferable would be something like this:
<asp:SqlDataSource ID="sdsSPs" runat="server"
ConnectionString="<%$ ConnectionStrings:AAPs_DevConnectionString %>"
SelectCommand="select SPECIFIC_NAME from #Path.INFORMATION_SCHEMA.ROUTINES where ROUTINE_TYPE = 'Procedure'">
<SelectParameters>
<asp:ControlParameter ControlID="lblDB" PropertyName="text" Name="Path" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
All of these trys have come back failing. There is no way for me to know ahead of time what the DB is as the user can select it. Is there a way to do this? and if so what is it? Thanks for your help!
You may have to write Dynamic Query/Procedure for your requirement,
Create Proc Get_From_Dynamic_Table
#tablename varchar(50)
as
begin
declare #sqlQuery nvarchar(100)
Set #sqlQuery = 'Select * from '+#tablename
exec sp_executesql #sqlQuery
end
Note: Code not tested.

mySQL .NET Connector, GridView not updating

Visual Studion 2012 Update 3 with mySQL .NET Connector 6.6.5.0
I cannot get a GridView to post back any updated data
My GridView is configured as:
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1" AutoGenerateColumns="False">
<Columns>
<asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
<asp:BoundField DataField="Name" />
<asp:BoundField DataField="Unit" />
<asp:BoundField DataField="Price" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mysqlConn %>" ProviderName="<%$ ConnectionStrings:mysqlConn.ProviderName %>"
SelectCommand="SELECT ID, Name, Unit, Price FROM product"
UpdateCommand="UPDATE product SET Name = ?, Unit = ?, Price = ? WHERE ID = ?">
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" Direction="Input" />
<asp:Parameter Name="Unit" Type="String" Direction="Input" />
<asp:Parameter Name="Price" Type="Single" Direction="Input" />
<asp:Parameter Name="ID" Type="Int64" Direction="Input" />
</UpdateParameters>
</asp:SqlDataSource>
The grid displays the list of products fine. However when I select Edit in the grid view and change a value and then click Update the resulting change is not posted back to the database. What am I doing wrong with my Update statement or parameters?
All pointers appreciated.
please use GridView1.DataBind();
then gridview will bind with new data.
please refer this link
http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
I was missing:
DataKeyNames="ID"
as part of the GridView definition which was preventing the UPDATE from happening.

using session variable in aspx page in sql query

I have the sqldatasource in aspx page and in the query I want to use one parameter i.e. coming from session.
Below is my code.Please help me out.
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id="Here I want to use session variable"" />
Try this
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id=#SessionVar">
<SelectParameters>
<asp:SessionParameter Name="SessionVar" SessionField="SessionVariableName" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
This MSDN article should get you what you need. Basically you would define your SelectCommand with the parameter placeholder, "?", and then define your SelectParameters collection with an entry for your SessionParameter.
Using parameters is rather simple:
<asp:SqlDataSource id="Employees" runat="server"
ConnectionString="<%$ ConnectionStrings:Northwind%>"
SelectCommand="SELECT LastName FROM Employees WHERE Title = #Title">
<SelectParameters>
<asp:ControlParameter Name="Title"
ControlID="DropDownList1"
PropertyName="SelectedValue"/>
</SelectParameters>
</asp:sqldatasource>
Just replace the value of parameter with your variable:
<%= Sessiom[variable_name] %>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=LAZY-PC;Initial Catalog=Test;Integrated Security=True"
ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [C] WHERE ([C#] = #column1)">
<SelectParameters>
<asp:SessionParameter Name="column1" SessionField="id" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
Johnny_D's answer is pretty much spot on, but I'd like to point out that there's a SessionParameter class that you can use for this:
<asp:SqlDataSource runat="server" ID="MySQLData2"
ConnectionString='<%$ConnectionStrings:ConnectionString %>'
ProviderName="MySql.Data.MySqlClient"
SelectCommand="SELECT * FROM tablename WHERE id= ?" />
<SelectParameters>
<asp:SessionParameter
Name="id"
SessionField="SessionVariableName"
DefaultValue="0" />
</SelectParameters>
</asp:SqlDataSource>

Sqldatasource where parameter

I have a sqldatasource, in select command i have #status parameter. The parameter take the value from the textbox at runtime.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details] WHERE ([status] = #status)"
FilterExpression="title='{4}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<SelectParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false"
Type="String" />
</SelectParameters>
</asp:SqlDataSource>
my problem is that when i run the page without entring the parameter in text box sqldatasource is not returing any row.
Looking at the documentation on MSDN, you have to alter how you have setup the SqlDataSource.
Try this:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [project_details]"
FilterExpression="title='{0}'"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
EnableCaching="True">
<FilterParameters>
<asp:ControlParameter ControlID="TextBox1" Name="status" PropertyName="Text" ConvertEmptyStringToNull="false" Type="String" />
</FilterParameters>
</asp:SqlDataSource>
I have removed the Where clause from the query as this will be applied by the filter expression. Also, I have changed the filter expression from title='{4}' to title='{0}'. The documentation states that the number is a placeholder to an item in the FilterParameters collection.
Updated
I have also changed the SelectParameters to FilterParameters
Update 2
I have created a working example to finish of this answer. This will filter the Title column using the text from the text box. If this text box is empty it will return all the rows from the table (a scary thought but OK for this example). It is querying the AdventureWorks database for which I set a connection string called AWorks.
<asp:SqlDataSource ID="SqlDataSource1"
ConnectionString="<%$ ConnectionStrings:AWorks %>"
SelectCommand="SELECT ContactId, Title, FirstName, LastName FROM Person.Contact"
FilterExpression="Title='{0}'"
runat="server">
<FilterParameters>
<asp:ControlParameter Name="Title" ControlID="txtTitle" PropertyName="Text" />
</FilterParameters>
</asp:SqlDataSource>
<asp:TextBox runat="server" Id="txtTitle"></asp:TextBox>
<asp:Button runat="server" UseSubmitBehavior="true" Text="Submit" />
<asp:GridView
DataSourceID="SqlDataSource1"
AutoGenerateColumns="false"
runat="server">
<Columns>
<asp:BoundField Visible="false" DataField="ContactId"></asp:BoundField>
<asp:BoundField Visible="true" DataField="Title"></asp:BoundField>
<asp:BoundField Visible="true" DataField="FirstName"></asp:BoundField>
<asp:BoundField Visible="true" DataField="LastName"></asp:BoundField>
</Columns>
</asp:GridView>
try a condition like this:
(#Status is null or #Status ='' Or Status = #Status)
set ConvertEmptyStringToNull="true" and then try....
Add CancelSelectOnNullParameter="false" to your SqlDataSource.

Categories

Resources