Solved

ASP.NET SQL Query example

Posted on 2010-08-23
6
643 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
6 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 14

Expert Comment

by:Dhanasekaran Sengodan
ID: 33507996
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 500 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

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

696 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