[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ASP.NET SQL Query example

Posted on 2010-08-23
6
Medium Priority
?
664 Views
Last Modified: 2013-11-27
I aplogize, I did not know exactly what I needed to post.  I saw this article which looks like what I'm trying to do:
http://www.mikesdotnetting.com/Article/64/Bind-Data-From-a-SqlDataSource-to-a-Label
I created a database.mdf with a table "Employees" with 3 columns, FirstName, LastName and EmployeeID

I copied this text into a Default.aspx Page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="Label1" runat="server" /> <asp:Label ID="Label2" runat="server" />


  <asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString2 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString2.ProviderName %>"
SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
OnSelecting="SqlDataSource1_Selecting">
</asp:SqlDataSource>
 
  <asp:SqlDataSource
ID="SqlDataSource2"
runat="server"
ConnectionString="<%$ ConnectionStrings:DatabaseConnectionString3 %>"
ProviderName="<%$ ConnectionStrings:DatabaseConnectionString3.ProviderName %>"
DatasourceMode="DataReader"
SelectCommand="SELECT [LastName], [FirstName] FROM [Employees] WHERE ([EmployeeID] = ?)"
OnSelecting="SqlDataSource2_Selecting">
</asp:SqlDataSource>

    </div>
    </form>
</body>
</html>


I copied this code into the default.aspx.vb page:

Imports System.Data
Imports System.Data.OleDb

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object,
      ByVal e As SqlDataSourceSelectingEventArgs)
        e.Command.Parameters("EmployeeID").Value = 2
    End Sub
    Protected Sub SqlDataSource2_Selecting(ByVal sender As Object,
      ByVal e As SqlDataSourceSelectingEventArgs)
        e.Command.Parameters("EmployeeID").Value = 2
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dvSql As DataView =
   DirectCast(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
        For Each drvSql As DataRowView In dvSql
            Label1.Text = drvSql("FirstName").ToString()
        Next

        Dim rdrSql As OleDbDataReader =
         DirectCast(SqlDataSource2.Select(DataSourceSelectArguments.Empty), OleDbDataReader)
        While rdrSql.Read()

            Label2.Text = rdrSql("LastName").ToString()
        End While
        rdrSql.Close()
    End Sub

End Class


I get this error message:

System.IndexOutOfRangeException was unhandled by user code
  Message=An SqlParameter with ParameterName 'EmployeeID' is not contained by this SqlParameterCollection.
  Source=System.Data
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
       at System.Data.Common.DbParameterCollection.get_Item(String parameterName)
       at _Default.SqlDataSource1_Selecting(Object sender, SqlDataSourceSelectingEventArgs e) in E:\WebSites\Simple SQL table lookup\Default.aspx.vb:line 9
       at System.Web.UI.WebControls.SqlDataSourceView.OnSelecting(SqlDataSourceSelectingEventArgs e)
       at System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
       at System.Web.UI.WebControls.SqlDataSource.Select(DataSourceSelectArguments arguments)
       at _Default.Page_Load(Object sender, EventArgs e) in E:\WebSites\Simple SQL table lookup\Default.aspx.vb:line 17
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException:

I know I'm doing something really dumb.  Can anyone please help?  Thanks
0
Comment
Question by:Bocefus
5 Comments
 
LVL 21

Expert Comment

by:chapmanjw
ID: 33507767
You have not specified where the EmployeeID parameter is coming from.  Here is sample code from Microsoft that shows you how to specify the parameters: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selectparameters.aspx
0
 
LVL 7

Expert Comment

by:kovilpattiBalu
ID: 33521532
hi,

In your select querys include "EmployeeID" as parameter.
Ie: SqlCommand cmdInsert = new SqlCommand(sqlInsert, sqlConnection);
cmdInsert.Parameters.Add("@EmployeeID", SqlDbType.NVarChar, 20, "EmployeeID");
0
 

Author Comment

by:Bocefus
ID: 33536893
kovilpattiBalu
Where would I add this?  
 
0
 
LVL 21

Accepted Solution

by:
chapmanjw earned 2000 total points
ID: 33537539
In between the  and  tags you would add a
 tag.  For examples, see: http://msdn.microsoft.com/en-us/library/z72eefad.aspx
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question