Think of ASP.Net as just another front end development tool. Upon some
event (on_click or Page_load, etc) you'll need to interact with a data
source. The contents of the data retrieved from a that source can be assign
to a datagrid object.
I can't guarantee the code below as it is really late and I'm typing this
adhoc from another one of my projects. In order to make it work, you'll
need a datagrid object on a web form called gridPA and a connection string
assigned to conn. Note the connection string can be defined in a web.config
file in the same directory as your ASPX page.
'On Page Load Event
Private Sub Page_Load(sender as Object, e as Event) Handles MyBase.Load
If Me.IsPostback = False Then 'First time on this page?
Dim ds as Dataset = GetDataSet() 'Go get the data
BindGrid(ds) 'fill the web
forms data grid object
End if
End Sub
'Get the data and assign it to an ADO.Net dataset
Private Function GetDataSet () as DataSet
Dim SQL as String = "SELECT [PACONTNUMBER] + [PAcontname] AS Expr1 FROM
dbo_PA01101"
Dim ConnectionString as String =
ConfigurationSettings.AppSettings("Connector")
Dim conn as New OleDbConnection(ConnectionString)
Dim cmd as New OleDbCommand(SQL,conn)
Dim adapter as New OleDbAdapter(cmd)
Dim dsPA As New DataSet()
Try
adapter.Fill(dsPA, "PA01101") 'Fill table PA01101 in dataset dsPA
with the results from the data adapter Select statement
Catch err as Exception
lblErrorLine.Text = "Error in GetDataSet - executing adapter.fill"
Finally
conn.close()
Return dsPA
End Try
End Sub
'Assign values in Datasets table to the Data Grid object on the web form
Private Sub BindGrid(ds As Dataset)
gridPA.Datasource = ds.Tables("PA01101")
me.DataBind()
End sub
WEB.CONFIG file
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<!-- Data Source = Server Name Inital Catalog = the database name
Integrated Security = SSPI is windows authentication --->
<add key = "Connector"
value = "Provider-SQLOLEDB.1;Data Source = localhost; Initial
Catalog = YourDatabaseName; Integrated Security=SSPI"/>
</appSettings>
<system.web>
<!-- Note You will want look these up to see what they do --->
<httpRuntime />
<pages />
<compilation />
<customErrors />
<authentication />
<authorization />
<identity />
<tracy />
<sessionState />
<httpHandlers />
<httpModules />
<globalization />
<compliation />
<system.web>
</configuration>
RockNRoll said:
Greetings,
I have a SQL Statement that runs fine in Access:
SELECT [PACONTNUMBER] & [PAcontname] AS Expr1 FROM dbo_PA01101
(It basically is a single row that concatenates PACONTNUMBER and PAcontname)
When I try to use this statement in asp.net, I am getting an error.
Are expressions allowed in SQL statements in ASP.NET? Can I make this work?
Are there any web sites or links that you can provide me to understanding
expressions in asp.net SQL statements? Thanks,