Solved

ASP.NET SQL Query example

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now