Showing posts with label date. Show all posts
Showing posts with label date. Show all posts

Thursday, March 22, 2012

Cant select Date

hi, my DatePost field in the database formated as2/15/2006 11:40:46 AM .i try to manually give a date (no) but it give me error. the error come from myReader!. help me to correct, thanks

no = "2152006"
Dim myConn As SqlConnection
Dim myCmd As SqlCommand = New SqlCommand
Dim myReader As SqlDataReader
Dim strSQL As String

myConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
strSQL ="SELECT Author FROM Booktbl WHERE cast(Date as datetime) ='" & no & "'"
myCmd.CommandText = strSQL
myConn.Open()
myCmd.Connection = myConn
myReader = myCmd.ExecuteReader
myReader.Read()

Author = myReader("Author")

myReader.Close()
myConn.Close()
lblShow.Text = Subject

Hello Sebastian,

Try adding slashes to your date string like this:

no = "2/15/2006"

Even better would be to use a variable of DateTime type like this:

Dim no As DateTime
no = "2/15/2006"

This should solve your problem.

Regards, Maurits

|||

If you used no="20060215" then your query would be correct, but it still would not return any records, because the datetime "2006-15-02 11:40:46" <> "2006-15-02 00:00:00".

no = "20060215" -- assume string is in YYYYMMDD format so SQL Server can implicitly convert it to a datetime for comparision (OR any string recognized by SQL Server's current language/culture setting)
Dim myConn As SqlConnection
Dim myCmd As SqlCommand = New SqlCommand
Dim myReader As SqlDataReader
Dim strSQL As String

myConn = New SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
strSQL ="SELECT Author FROM Booktbl WHERE cast(Date as datetime) ='" & no & "'"
myCmd.CommandText = strSQL
myConn.Open()
myCmd.Connection = myConn
myReader = myCmd.ExecuteReader
myReader.Read()

Author = myReader("Author")

myReader.Close()
myConn.Close()
lblShow.Text = Subject

I'm going to assume you know how to make no a datetime variable instead of a string, but assuming it is a datetime with a time portion being midnight, this should do what you want:

Dim myConn As new SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
myConn.open
Dim myCmd As SqlCommand = New SqlCommand("SELECT Author FROM Booktbl WHERE cast(Date as datetime) >=@.no AND cast(Date as datetime)<dateadd(d,1,@.no)",myConn)
myCmd.parameters.add("@.no",sqldbtype.datetime).value=no
lblShow.Text = myCmd.executeScalar
myConn.Close()

Actually, the above code will work even if no is string that contains a datetime in a format that is understood by the culture the ASP.NET thread is running too. (For example "2/15/2006" if the current culture is US-en, or "15-02-2006" if the current culture is FR-fr", etc.)

|||Two things to keep in mind when working with datetime column types.

1. Datetime type contains two components: date and time. So, when specifying WHERE clause, you have to consider the time component as well. By default, datetime's time component is "12:00:00 AM". So, if you're trying to filter for date of "02-20-2006', the something similar to following would be used:

... WHERE DATE_COL >= '02-20-2006' AND DATE_COL< '02-21-2006'

(notice the less-than sign and the date of one more than the one intended in query)

or

... WHERE DATE_COL >= '02-20-2006 12:00:00 AM' AND DATE_COL<= '02-20-2006 11:59:59 PM'

2. As in the example above, you need to provide a specific format of date string. Your value of "2152006" is confusing to the translation engine, so transform it to something like "mm-dd-yyyy", "mm/dd/yyyy" or even "yyyymmdd" which is the ISO version.|||

WHERE DATE_COL >= '02-20-2006' AND DATE_COL< '02-21-2006'

is really what you should use, the statement

WHERE DATE_COL >= '02-20-2006 12:00:00 AM' AND DATE_COL<= '02-20-2006 11:59:59 PM'

actually has two issues. First, if you use that with a datetime column, since the datetime field is accurate to approximately 1/300th of a second, there is really a 1-second gap that the query will not find. You could conceivably do something like:

WHERE DATE_COL >= '02-20-2006 12:00:00 AM' AND DATE_COL<= '02-20-2006 11:59:59.997 PM'

but that assumes you know the exact highest time value that is storable in a datetime field, which I prefer not to use (assume). It's inviting problems on upgrades, conversion, or database migration to another platform. Secondly, if you tried to do that with a smalldatetime field, SQL Server will convert the strings to a smalldatetime, and since that is only accurate to 2 seconds, it will round your string to the nearest 2-second interval (Since it is half way between, it will round UP), giving you 02-21-2006 00:00:00, which will include any smalldatetimes that are part of midnight the next day, also very yucky.

|||

jcasp wrote:

Two things to keep in mind when working with datetime column types.

... WHERE DATE_COL >= '02-20-2006' AND DATE_COL< '02-21-2006'

or

... WHERE DATE_COL >= '02-20-2006 12:00:00 AM' AND DATE_COL<= '02-20-2006 11:59:59 PM'


I'd definitely stick with that first WHERE clause. The second one is not taking milliseconds into consideration. And I prefer using the ISO version of the date '20060215' as it is unanmbiguous.

Motley wrote:


Dim myCmd As SqlCommand = New SqlCommand("SELECT Author FROM Booktbl WHERE cast(Date as datetime) >=@.no AND cast(Date as datetime)<dateadd(d,1,@.no)",myConn)


You want to stay away from this approach Don't use a function against a column like that; it really increases the overhead in running the query as the fuction has to be performed against every row. Stick with the approach suggested by jcasp.|||Correction noted. One should keep in mind that datetime column type does have higher precision than the second component (goes to milliseconds). Very easy to overlook, but can definitely come into play. Good catch.|||

tmorton wrote:

jcasp wrote:

Two things to keep in mind when working with datetime column types.

... WHERE DATE_COL >= '02-20-2006' AND DATE_COL< '02-21-2006'

or

... WHERE DATE_COL >= '02-20-2006 12:00:00 AM' AND DATE_COL<= '02-20-2006 11:59:59 PM'


I'd definitely stick with that first WHERE clause. The second one is not taking milliseconds into consideration. And I prefer using the ISO version of the date '20060215' as it is unanmbiguous.

Motley wrote:


Dim myCmd As SqlCommand = New SqlCommand("SELECT Author FROM Booktbl WHERE cast(Date as datetime) >=@.no AND cast(Date as datetime)<dateadd(d,1,@.no)",myConn)


You want to stay away from this approach Don't use a function against a column like that; it really increases the overhead in running the query as the fuction has to be performed against every row. Stick with the approach suggested by jcasp.

Actually, what I wrote is the only method mentioned here that will actually work, assuming that the field "Date" is actually a varchar field. Since the original poster said his "PostDate" field had a format of "MM/DD/YYYY HH:MM:SS PM", and datetimes don't have a format, this was my assumption. Of course, if the original poster was incorrect, and the field is actually a datetime, then just drop the cast portion of my command. Since the DateAdd function is a deterministic function, and it's input is based on the parameters, SQL Server will only need to execute this once instead of every row.

|||ok, before i select the author record, i have insert a date record into the table. this is how i do.

Dim Date As Date = Now()

Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
Dim myCommand As New SqlCommand("INSERT INTO Booktbl (Author, Date) VALUES('" & strAuthor & "', '" & Date & "')", myConnection)
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()

Catch ex As SqlException
lblMsg.Text = " Error"
End Try
End Sub

End insert section. the data type used to store date is datetime. the record look like this 2/12/2006 11:40:40 AM

when i try to retrive back the record base on the date it give me error. i also declare the no as date, datetime or string and put it "2/12/2006" or some other format it still not work. i have try one example to put the no = "2/12/2006 11:40:40 AM" then it work, but what i need is the date not the time.

anymore figure out what happen??|||Dim MyDate As Datetime = Now()

Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
Dim myCommand As New SqlCommand("INSERT INTO Booktbl (Author, Date) VALUES(@.author,@.MyDate)", myConnection)
myCommand.parameters.add("@.author",sqdbtype.varchar).value=strAuthor
myCommand.parameters.add("@.MyDate",sqldbtype.datetime).value=MyDate
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()

Catch ex As SqlException
lblMsg.Text = " Error"
End Try
End Sub|||Motley, i have used your code to insert data to the table. now i want to select the info and display it. This is how i do it!

Dim no As DateTime
no = "2/22/2006"

Dim myConnection As New SqlConnection(ConfigurationSettings.AppSettings("ConnStr"))
Dim myCommand As New SqlCommand("SELECT Author FROM Booktbl WHERE Date = @.no", myConnection)
myCommand.Parameters.Add("@.no", SqlDbType.DateTime).Value = no
myConnection.Open()

Dim myReader As SqlDataReader
myReader = myCommand.ExecuteReader()
myReader.Read()

Author = myReader("Author")

myReader.Close()
myConnection.Close()
lblShow2.Text = Author

it give me this error message "Invalid attempt to read when no data is present."why? why i can't retrive the data? i been work on this few days ago, hope u can help me. thanks
|||

Because the datetimes aren't exactly the same.

SELECT Author FROM Booktbl WHERE dateadd(d,0,datediff(d,0,Date)) = dateadd(d,0,datediff(d,0,@.no))

is the select statement you want if you just want the date portions to match.

SELECT Author FROM Booktbl WHERE Date>=dateadd(d,0,datediff(d,0,@.no)) AND Date<dateadd(d,1,datediff(d,0,@.no))

is the same idea, but will execute much faster, it's looking for a datetime >= to the beginning of the day in @.no through (but not including) the beginning of the day after @.no. (Between 2/22/2006 @. midnight through 2/23/2006 @. midnight)

sql

Sunday, March 11, 2012

Can't run SP with GETDATE()

I'm having a problem running this stored procedure that is supposed to count
the "Failed Domains" based on the beginning date and end date.
I'd like to simply run this sp with getdate()-2 and getdate-1, but when I
run this:
Exec dbo.PROC_SuccessCountByDate getdate()-1, (getdate()
I get the error
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ')'.
The Stored Procedure:
CREATE PROC PROC_SuccessCountByDate
@.begdate datetime,
@.enddate datetime
as
select count(domain_) as 'Failed Domain Count', T1m.domain_
from Table1 T1
join Table2 T2
on T2.memberid = T1.memberid_
where T2.completionstatusid in (301, 303)
and T2.finalattempt > @.begdate
and T2.finalattempt < @.enddate
group by T1.domain_
having count(domain_)>5
order by 'Failed Domain Count' descDeclare @.Today datetime
Declare @.Yesterday datetime
SET @.Today = Getdate()
SET @.Yesterday = DATEADD(dd,-1,Getdate())
Exec dbo.PROC_SuccessCountByDate @.Yesterday,@.Today
HTH, Jens SUessmeyer.
"savvy95" <savvy95@.discussions.microsoft.com> schrieb im Newsbeitrag
news:058453F4-9FE1-440E-ADB0-67D284B6957C@.microsoft.com...
> I'm having a problem running this stored procedure that is supposed to
> count
> the "Failed Domains" based on the beginning date and end date.
> I'd like to simply run this sp with getdate()-2 and getdate-1, but when I
> run this:
> Exec dbo.PROC_SuccessCountByDate getdate()-1, (getdate()
> I get the error
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near ')'.
>
> The Stored Procedure:
> CREATE PROC PROC_SuccessCountByDate
> @.begdate datetime,
> @.enddate datetime
> as
> select count(domain_) as 'Failed Domain Count', T1m.domain_
> from Table1 T1
> join Table2 T2
> on T2.memberid = T1.memberid_
> where T2.completionstatusid in (301, 303)
> and T2.finalattempt > @.begdate
> and T2.finalattempt < @.enddate
> group by T1.domain_
> having count(domain_)>5
> order by 'Failed Domain Count' desc
>|||Yes, you can;t pass an "Expression". like [getdate() - 1], as a parameter
to a Stored Proc.
You can only pass constant values, or variables (either the variable value
or address)
So in your case, create a variable (Declare @.MyDateTime DateTime) and set
the value of that variableto getdate() - 1, and then make your call using th
e
variable...
Exec dbo.PROC_SuccessCountByDate @.MyDateTime, getdate()
And get rid of the extra parentheses after the comma
Exec dbo.PROC_SuccessCountByDate getdate()-1, (getdate()
"savvy95" wrote:

> I'm having a problem running this stored procedure that is supposed to cou
nt
> the "Failed Domains" based on the beginning date and end date.
> I'd like to simply run this sp with getdate()-2 and getdate-1, but when I
> run this:
> Exec dbo.PROC_SuccessCountByDate getdate()-1, (getdate()
> I get the error
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near ')'.
>
> The Stored Procedure:
> CREATE PROC PROC_SuccessCountByDate
> @.begdate datetime,
> @.enddate datetime
> as
> select count(domain_) as 'Failed Domain Count', T1m.domain_
> from Table1 T1
> join Table2 T2
> on T2.memberid = T1.memberid_
> where T2.completionstatusid in (301, 303)
> and T2.finalattempt > @.begdate
> and T2.finalattempt < @.enddate
> group by T1.domain_
> having count(domain_)>5
> order by 'Failed Domain Count' desc
>|||declare @.bd datetime
declare @.ed datetime
set @.bd = dateadd(day, -1, getdate())
set @.ed = getdate()
Exec dbo.PROC_SuccessCountByDate @.bd, @.ed
...
AMB
"savvy95" wrote:

> I'm having a problem running this stored procedure that is supposed to cou
nt
> the "Failed Domains" based on the beginning date and end date.
> I'd like to simply run this sp with getdate()-2 and getdate-1, but when I
> run this:
> Exec dbo.PROC_SuccessCountByDate getdate()-1, (getdate()
> I get the error
> Server: Msg 170, Level 15, State 1, Line 1
> Line 1: Incorrect syntax near ')'.
>
> The Stored Procedure:
> CREATE PROC PROC_SuccessCountByDate
> @.begdate datetime,
> @.enddate datetime
> as
> select count(domain_) as 'Failed Domain Count', T1m.domain_
> from Table1 T1
> join Table2 T2
> on T2.memberid = T1.memberid_
> where T2.completionstatusid in (301, 303)
> and T2.finalattempt > @.begdate
> and T2.finalattempt < @.enddate
> group by T1.domain_
> having count(domain_)>5
> order by 'Failed Domain Count' desc
>|||This is why I love this community; so many willing to help. Thanks. All of
you had the same solution.
Thanks again
"Jens Sü?meyer" wrote:

> Declare @.Today datetime
> Declare @.Yesterday datetime
> SET @.Today = Getdate()
> SET @.Yesterday = DATEADD(dd,-1,Getdate())
> Exec dbo.PROC_SuccessCountByDate @.Yesterday,@.Today
> HTH, Jens SUessmeyer.
> "savvy95" <savvy95@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:058453F4-9FE1-440E-ADB0-67D284B6957C@.microsoft.com...
>
>|||Perhaps one time there will be an issue which you can also solve in here...
:-)
"savvy95" <savvy95@.discussions.microsoft.com> schrieb im Newsbeitrag
news:0BA224B0-F049-4E46-8BBD-6113DCCAF216@.microsoft.com...
> This is why I love this community; so many willing to help. Thanks. All
> of
> you had the same solution.
> Thanks again
> "Jens Smeyer" wrote:
>|||i want to make SP that enters values into my table, which has the default
value of the date column to getdate
so, if i didn't enter a value it will take the default value of the column
how can i describe this in the sp declaration ?
my sp is as follows :
---
create procedure add_new_abstract
@.abs_name nvarchar(20) ,
@.id_reg nvarchar(10),
@.id_topic nvarchar(10),
@.id_stat nvarchar(10),
@.abst nvarchar(20) ,
@.job_id nvarchar(20),
@.date_abs datetime
as
insert abstract
values (cast(cast(rand()*9285 as int)as
nvarchar(10)),@.abs_name,@.id_reg,@.id_topi
c,@.id_stat,@.abst,@.job_id,@.date_abs)
go
---
so, when i execute it with the getdate() it raise the error specified in
this discussion
and when i neglect it so that it takes the value as the default value of the
column , it asks for it
so, how can i solve this ?
thanx for ur help
--
regards
Maidoo.
"Jens Sü?meyer" wrote:

> Perhaps one time there will be an issue which you can also solve in here..
.
> :-)
>
> "savvy95" <savvy95@.discussions.microsoft.com> schrieb im Newsbeitrag
> news:0BA224B0-F049-4E46-8BBD-6113DCCAF216@.microsoft.com...
>
>|||On Fri, 10 Jun 2005 10:59:27 -0700, maidoo wrote:

>i want to make SP that enters values into my table, which has the default
>value of the date column to getdate
>so, if i didn't enter a value it will take the default value of the column
>how can i describe this in the sp declaration ?
Hi maidoo,
Change your proc like this:
create procedure add_new_abstract
@.abs_name nvarchar(20) ,
@.id_reg nvarchar(10),
@.id_topic nvarchar(10),
@.id_stat nvarchar(10),
@.abst nvarchar(20) ,
@.job_id nvarchar(20),
@.date_abs datetime = null
as
insert abstract
values (cast(cast(rand()*9285 as int)as
nvarchar(10)),@.abs_name,@.id_reg,@.id_topi
c,@.id_stat,@.abst,@.job_id,
COALESCE(@.date_abs, CURRENT_TIMESTAMP))
go
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||create procedure add_new_abstract
@.abs_name nvarchar(20) ,
@.id_reg nvarchar(10),
@.id_topic nvarchar(10),
@.id_stat nvarchar(10),
@.abst nvarchar(20) ,
@.job_id nvarchar(20),
@.date_abs datetime
as
SET @.date_abs = ISNULL(@.date_abs, GETDATE())
insert abstract
values (cast(cast(rand()*9285 as int)as
nvarchar(10)),@.abs_name,@.id_reg,@.id_topi
c,@.id_stat,@.abst,@.job_id,@.date_abs)
go
--
Mike Epprecht, Microsoft SQL Server MVP
Zurich, Switzerland
IM: mike@.epprecht.net
MVP Program: http://www.microsoft.com/mvp
Blog: http://www.msmvps.com/epprecht/
"maidoo" <maidooalex@.msn.com> wrote in message
news:B70444AF-0A65-416E-91DB-05E4D911809E@.microsoft.com...
>i want to make SP that enters values into my table, which has the default
> value of the date column to getdate
> so, if i didn't enter a value it will take the default value of the column
> how can i describe this in the sp declaration ?
> my sp is as follows :
> ---
> create procedure add_new_abstract
> @.abs_name nvarchar(20) ,
> @.id_reg nvarchar(10),
> @.id_topic nvarchar(10),
> @.id_stat nvarchar(10),
> @.abst nvarchar(20) ,
> @.job_id nvarchar(20),
> @.date_abs datetime
> as
> insert abstract
> values (cast(cast(rand()*9285 as int)as
> nvarchar(10)),@.abs_name,@.id_reg,@.id_topi
c,@.id_stat,@.abst,@.job_id,@.date_abs
)
> go
> ---
> so, when i execute it with the getdate() it raise the error specified in
> this discussion
> and when i neglect it so that it takes the value as the default value of
> the
> column , it asks for it
> so, how can i solve this ?
> thanx for ur help
> --
> regards
> Maidoo.
>
> "Jens Smeyer" wrote:
>

Sunday, February 12, 2012

Cant insert NULL to DateTime field

Hi I'm using DetailView and I have a text box which show the date. I have formated the date as shortDate {0:d} format. I want to insert/update null if that text box is empty.

I have tried putting null value in my Update statement in sqlDataSource. And I'm getting error saying can't convert varchar to smalldatetime.

If I don't set null value as above, some large date (01/01/2033) has been inserted in my database.

Can anyone help me?

Moe

insert DBNull.Value not "null"

Hope this help|||Thanks a lot.. :)|||Dear,Can you explain where I put this code? I am newbie and have the same with formview, and I wonder where to enter this DBNULL?Can you help please?|||

Here is sample code:

sqlStmt ="insert into Emp (FirstName,LastName,Date) Values (?,?,?) ";conString ="Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data source=localhost";cn =new OleDbConnection(conString);cmd =new OleDbCommand(sqlStmt, cn) ;cmd.Parameters.Add(new OleDbParameter("@.FirstName", OleDbType.VarChar, 40));cmd.Parameters.Add(new OleDbParameter("@.LastName", OleDbType.VarChar, 40));cmd.Parameters.Add(new OleDbParameter("@.Date", OleDbType.Date)); cmd.Parameters["@.FirstName"].Value = txtFirstName.Text;cmd.Parameters["@.LastName"].Value = txtLastName.Text;if ((txtDate.Text =="") ){cmd.Parameters["@.Date"].Value = DBNull.Value;}else{cmd.Parameters["@.Date"].Value = DateTime.Parse(txtDate.Text);}cn.Open();cmd.ExecuteNonQuery();Label1.Text ="Record Inserted Succesfully";

Can't group on Month part of the date.

Hi everyone,
I have a report with Period field that I use on a graph as Category
field (horozontal axe.) I want to group on the month part of the
Period. In my sql query Period is DATETIME. I found a similar topic
here and used this expession to group on month:
=DatePart("MM", Fields!Period.Value)
I also sorted by Period to preserve date-order.
I am getting the following error in preview:
The group expression for the grouping 'chart1_CategoryGroup1' contains
an error: Argument "Interval" is not a valid value.
Here is a format of my Period as it comes from SQL Server:
2004-02-29 00:00:00.000
Any ideas what is wrong?
Thanks in advance!
StanI just found my problem.
this
=DatePart("MM", Fields!Period.Value) shoild be like that:
=DatePart("M", Fields!Period.Value)
One single m!
Stan|||There is another problem. My data runs from February, 2004 to October,
2005. When I group on month my graph shows only 12 months. I don't see
data from the mid-end 2005!!! Is this s bug or a feature of RS'
Stan|||You need to group on the combination of year and month - otherwise data
from both Feb 2004 and Feb 2005 will be grouped together (and similarly
for other months, of course).
Something like =DatePart("yyyy", Fields!Period.Value) & " " &
DatePart("m", Fields!Period.Value)
You may need to convert this to a string, with a leading zero for the
month, for it to work correctly.|||Thanks! I did just that. In Group On expression for the Period in my
graph I entered:
=Year(Fields!Period.Value)*100 + Month(Fields!Period.Value)
so that I group on a combination of year and month.
Stan

Friday, February 10, 2012

Cant get recordID from Database Table

Hi

I have the same problem but those posts are no longer there. Are you able to help me out?

I couldn't work it out so I created a date/time field called StartDate for when each record is entered. I was trying to use that to grab the particular eventID.

Dim EventID =""

tbEventIDTest.Text =""

Dim EventDataSource1AsNew SqlDataSource()

EventDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString

EventDataSource1.SelectCommandType = SqlDataSourceCommandType.Text

EventDataSource1.SelectCommand ="SELECT EventID FROM Event "WHERE ([StartDate] = @.StartDate)"

EventID = EventDataSource1.SelectParameters.Item(EventID)

tbEventIDTest.Text = EventID

Thanks, any help will be appreciated.

Hi,

Try following

EventID = EventDataSource1.SelectParameters.Item(EventID).DefaultValue

Swati

|||

Hi and thanks for your help.

I tried it but it didn't work. DefaultVAlue wasn't even one of the default options. There was 'equals', 'GetHash', 'GetType', 'ReferenceEquals', 'To String'.

I was thinking that maybe my StartDate in the label and my StartDate in the database don't match. I might try adding a default value to test.

But I don't think that that is the problem.

Do you have any other ideas?

|||So you just want to get the EventID from the sqldataource, right? If so you can try:

Dim dssa As ?new DataSourceSelectArguments()

Dim dt As new DataTable
dt= ((DataView)EventDataSource1.Select(dssa)).ToTable()

EventID= dt.Rows(0)(0).ToString()

If?you just want to retrieve a single value, it's more easier to use SqlCommand:

Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString())

Dim cmd As New SqlCommand( "SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)", conn)

conn.Open()
EventID= cmd.ExecuteScalar()|||

Hi

I have entered in all of the code but it is asking me to declear 'DataView'. I have tried 'Integer' and 'String' but it doesn't like these. Can you help me out with this?

Cheers

George

|||

I've been experimenting. DataView is now and the site loads but now I get an error that says..........

Index was outside the bounds of the array.

Here is my code:

Dim EventDataSource1AsNew SqlDataSource()

EventDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString

Dim dssaAsNew DataSourceSelectArguments()

Dim EventIDAsString =""

Dim DataView =""

Dim dtAsNew Data.DataTable

dt = ((DataView)(EventDataSource1.Select(dssa))).ToTable()

EventID = dt.Rows(0)(0).ToString()

Dim connAsNew Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString())

Dim cmdAsNew Data.SqlClient.SqlCommand("SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)", conn)

conn.Open()

EventID = cmd.ExecuteScalar()

thanks

George

|||Hi George, it seems you forget to set SelectCommand for EventDataSource1, so no row returned by the Select methodSmile|||

Sorry about this but I still get the same error. Can you help me further, please?

The error says: Index was outside the bounds of the array.

For this line of code: dt = ((DataView)(RaffleDataSource1.Select(dssa))).ToTable()

Here is my whole code:

Dim EventDataSource1AsNew SqlDataSource()

EventDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString

Dim dssaAsNew DataSourceSelectArguments()

Dim EventIDAsString =""

Dim DataView =""

Dim dtAsNew Data.DataTable

dt = ((DataView)(EventDataSource1.Select(dssa))).ToTable()

EventID = dt.Rows(0)(0).ToString()

Dim connAsNew Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString())

Dim cmdAsNew Data.SqlClient.SqlCommand("SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)", conn)

EventDataSource1.SelectCommand = ("SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)")

conn.Open()

EventDataSource1.SelectCommand.ToString(EventID)

EventID = cmd.ExecuteScalar()

tbEventIDTest.Text = EventID

|||Try to set SelectCommand for?EventDataSource1?before?calling?EventDataSource1.Select(dssa)Smile:

==============Code==============
Dim EventDataSource1 As New SqlDataSource()

EventDataSource1.ConnectionString = ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString

Dim dssa As New DataSourceSelectArguments()

Dim EventID As String = ""

Dim DataView = ""

Dim dt As New Data.DataTable

Dim conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ASPNETDBConnectionString").ToString())

Dim cmd As New Data.SqlClient.SqlCommand("SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)", conn)

EventDataSource1.SelectCommand = ("SELECT EventID FROM Event WHERE ([StartDate] = @.StartDate)")

conn.Open()

dt = ((DataView)(EventDataSource1.Select(dssa))).ToTable()

EventID = dt.Rows(0)(0).ToString()

'EventDataSource1.SelectCommand.ToString(EventID)

EventDataSource1.SelectParameters.Add("@.StartDate",StartDate)
' StartDate is a variable which will be passed as parameter for the SqlCommand

EventID = cmd.ExecuteScalar()

tbEventIDTest.Text = EventID
==============================

Note in the code above the EventID has been assigned?with?same?value twice, you can just use one of the 2 methods?(SqlCommand?or?SqlDataSource) to set it.

For more information, you can refer to:
Using Parameters with the SqlDataSource Control|||Thank you for the help and thank you for breaking it down fo me.