Showing posts with label design. Show all posts
Showing posts with label design. Show all posts

Friday, March 30, 2012

How to control the loading of SqlDataSource?

I have an SqlDatSource that I have fully setup at design time, but I don't want it to open and load as soon as the page loads, instead I want to open it based on a condition.

How do you do that with a DataSource?

I know that I can simply remove the Select query, and then set it at run time, but I'm looking for something better, that allows me to have the Select query set at design time, in part because I have a lot of parameters.

Usually you have a control such as GridView or DetailsView that is connected to the SqlDataSource through its DataSourceId attribute. This will cause it to execute the query. If you leave the attribute empty the query will not be executed. From code you can set the DataSource and then call DataBind(). That will cause the SqlDataSource to execute:

GridView1.DataSource = SqlDataSource1;GridView1.DataBind();// Now the query is executed
|||

Within the SqlSataSource.Selecting event, you will have access to the Cancel property which you can set based on your conditions. If Cancel is set to True, then the Select event will not occur.

|||

while michielvoo's way is probably better, another way you can get the date to not display is to just set the GridView's Visible="false" initially, if you want the datasourceID property of the sqldatasource to be defined in the contol creation. Then, in your button_click event or whatever you do to get the data, you do:

VB:

GridView1.DataBind()
GridView1.Visible = True

C#:

GridView1.DataBind();
GridView1.Visible = True;

Monday, March 26, 2012

How to connect to SQL Database, Create tables

Hi All, 1st of all happy New Year to all asp.net forum members
I am new at asp.net. I want to design a websiteusing asp.net as frontend and sql database as backend. I am able toconnect and add,update as well delete records when I use MsAcess andAsp.net using the following connection strings...
**********
sub Page_Load
dim dbconn,sql,dbcomm,dbread
dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;data source=" & server.mappath("/database/northwind.mdb"))
dbconn.Open()
sql="SELECT * FROM customers where city LIKE 'Berlin' order by city ASC"
dbcomm=New OleDbCommand(sql,dbconn)
dbread=dbcomm.ExecuteReader()
customers.DataSource=dbread
customers.DataBind()
dbread.Close()
dbconn.Close()
end sub
**********

But when I try to connect to sql database using the following connection strings I am unable to do so...

**************
SqlConnection myConnection = new SqlConnection("server=PLATINUM\VSdotNET;database=pubs;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter(" * from Authors", myConnection);

DataSet ds = new DataSet();
myCommand.Fill(ds, "Authors");

MyDataGrid.DataSource=ds.Tables["Authors"].DefaultView;
MyDataGrid.DataBind();

**************
I have written the servername as "PLATINUM\VSdotNET" because when Iinstalled SQL SERVER 2000 I found a tray icon where the server name wasdisplaying the same (my computer name is PLATINUM).
When I used webmatrix I enterd the same server name and windowsauthentication I was able to create a database but How to createtable...

Please help me out
Thanks in advance...Please explain "I am unable to do so". Are you receiving an error message?|||Actually I don't know whether the connection string I am using is correct or not...
Because I am unable to create tables using the connection strigns what I told before...

when I try running this page I get the error message like
Compiler Error Message:CS1009: Unrecognized escape sequence
and the compiler shows a error in the connection string , So as far I guess there is problem with the connection string only.

If I get a sample page for what I am trying to do i.e connecting to SQLdatabase, Creating tables as well as accessing table my problem will besolved.
Thanks for your time.


|||Well, I always use this resource for building connection strings:http://www.connectionstrings.com/ Check out the SqlConnection (.NET) options under SQL Server. Yourproblem *might* be that you should use True, not Yes, forTrusted_Connection. In any case, I would try to stick as closelyas possible to the advice listed there.|||* from Authors is not a valid SQL Statement. Try SELECT * FROM Authors