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.

No comments:

Post a Comment