Category Archives: SqlDataSource

Sending emails to members on a database

I use MS Visual Web Developer 2008 Express edition with VB and SQL Server Express.

I have an SQL database of over 300 club members details which contain(amongst other things) the following details:

Forename, Surname, Email address, Full postal address

I want to send an email to each member showing their forename, Surname, and postal address so that they can confim that these are correct.

Can anyone help me on how to do this?  Can I run this directly from an aspx page or does it have to be some form of scheduled process(I've merely seen reference to scheduled processes but know nothing about how to do this)?


Ta


John




Look at this code:


Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim conn As New SqlConnection("Data Source=SQL;Initial Catalog=Mytable;User Id=sa;Password=PASSWORD;")
        conn.Open()


















        Dim da As New SqlCommand("select firstname, lastname, email from CUSTOMERS", conn)
       



        Dim mySqlDataAdapter As New SqlDataAdapter()
        mySqlDataAdapter.SelectCommand = da


        Dim ds As New DataSet
        mySqlDataAdapter.Fill(ds)
        Dim dt As DataTable = ds.Tables(0)
 



              



                    Dim mail As New MailMessage()


                    mail.From = (New MailAddress("noreply@comapny.se", "John Smith"))








                    mail.ReplyToList.Add(New MailAddress("noreply@comapny.se", "John Smith"))
                    mail.BodyEncoding = Encoding.GetEncoding("x-IA5-Swedish")


                    mail.BodyEncoding = Encoding.UTF8
                    mail.SubjectEncoding = Encoding.UTF8


                    'set the content
                    mail.Subject = "Welcome!"


                    Dim name As String
                    name = drv("firstname")


                    'first we create the Plain Text part
                    Dim plainView As AlternateView = AlternateView.CreateAlternateViewFromString("Hello" & name!", Nothing, "text/plain")




                                 





                    Dim htmlView As AlternateView = AlternateView.CreateAlternateViewFromString("Hej " & name!" & "<br><br>Thanks for joining us", System.Text.Encoding.UTF8, "text/html")


            

                    mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
                    'add the views
                    mail.AlternateViews.Add(plainView)
                    mail.AlternateViews.Add(htmlView)


                    mail.IsBodyHtml = True




 

                    Dim addresses As String
                    addresses = drv("email")
                    mail.To.Add(addresses )


                    'send the message
                    Dim smtp As New SmtpClient("backup")
                    smtp.Credentials = New NetworkCredential("USERNAME", "PASSWORD")
                    smtp.Send(mail)
                  

      

        Next




Conn.Close()
END sub




If any querstion - just ask.

Sorry but I really couldn't understand this.

I've no C experience or any experience of stored procedures (My Service provider has previously told me they do not allow stored procedures).

Thanks anyway


John

Tried this code but it gave errors "Type sqlconnection is not defined" on all the SQl dims.

I've only ever used sqldatasource connections in the aspx page previously so this is all new to me(As per code below)

<asp:SqlDataSource ID="SqlMembers" runat="server"
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>"

Any advice on adapting the code?

Ta.

John




Look att your web.config file. There you see how the connectionstring is made. The same way applies to connection to SQL databases in code behind.

Sorry but I still don't understand.  In web.config I have

<connectionStrings>
        <clear />
        <add name="ConnectionString" connectionString="Data Source=Club-SQL-03;Database=Clubdb1;             User Id=ClubId; Password=PasswordT3"  />
        <add name="LocalSqlServer" connectionString="Data Source=Club-SQL-03;Database=Clubdb1;             User Id=ClubId; Password=PasswordT3"  />
    </connectionStrings>

How does this help with the fact that sqlconnection, etc are being reported as type not defined?

What do I actually change in the VB code?


Ta.


John


[RESOLVED] Can't see TreeView from code-behind code page

I have an aspx page with a TreeView control named TreeView1.  in the Page_Load method I am trying to collapse all nodes except for the current node.  I used this code in the Page_Load method:

TreeView1.CollapseAllNodes();
TreeView1.SelectedNode.Expand();

When I try to run this I get the following error:

CS0103: The name 'TreeView1' does not exist in the current context

I am using a .NET 2.0 website and can't seem to reference controls in my .aspx page from within my code-behind page. I tried adding an instance of the TreeView before my Page_Load method call in the code behind, but, it still doesn't create a reference.  What am I missing?

Assuming you haven't misspelled "TreeView1", it looks to me like your .aspx isn't connected to your code-behind.

I'd try:

  • rebuilding the entire solution
  • closing Visual Studio, rebooting computer [please, don't say it, I know, but sometimes...]
  • excluding the page in question from your solution, and then adding it in as a new item, copying markup and code from old to new

If all that fails, could you please post all your markup and code - the entirel page?  With that, I'll have a better chance at spotting something. 

(It makes things a lot more readable if you use the Insert Code tool. If you need instructions, see my How to use the Forums' "Insert Code" tool. post.) 

I have tried your suggestions and didn't get much further.  I think I am missing something a little less complex than that.  This is a .NET 2.0 site.  When I tried to reference the controls in the code-behind page I used this approach:

TreeView TreeView1 = (TreeView)Page.FindControl("TreeView1");


However, when I tried to use tab completion after typing (TreeView)Page. it would not provide the FindControl method.  Is there something about the project config I need to look at?  I have never had this much difficulty referencing code from the code-behind.

FindControl executes at run-time, and should only be used for dynamically created controls, like those inside ItemTemplates, etc.

Assuming that you've created TreeView1 using basic markup, you should be able to replace the above line with this "TreeView TreeView1 = this.TreeView1;".   Better yet, you should be able to refer directly to this.TreeView1 wherever you need to.

If this doesn't help, would you mind posting all your relevant markup and code?

The following page doesn't specifically look for TreeView1, but, it's the same problem.  I cannot see controls from the code-behind.  Here is the .aspx code:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="locked_batches.aspx.cs" Inherits="Environments_Model_locked_batches" Src="~/Environments/Model/locked_batches.aspx.cs" %>


<!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 id="Head1" runat="server">
    <title></title>
    <link href="../../Stylesheets/default.css" rel="Stylesheet" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div id="top"><span class="fiserv">eFichency</span> Reference</div>
        <div id="preferences">
            <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                onselectedindexchanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
        </div>
        <div id="content">
            <table>
                <tr>
                    <td>
                        <asp:SiteMapDataSource ID="SiteMapDataSource" runat="server" />
                        <asp:TreeView ID="TreeView1" runat="server" DataSourceID="SiteMapDataSource"
                            ImageSet="Msdn" NodeIndent="10">
                            <HoverNodeStyle BackColor="#CCCCCC" BorderColor="#888888" BorderStyle="Solid"
                                Font-Underline="True" />
                            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black"
                                HorizontalPadding="5px" NodeSpacing="1px" VerticalPadding="2px" />
                            <ParentNodeStyle Font-Bold="False" />
                            <SelectedNodeStyle BackColor="White" BorderColor="#888888" BorderStyle="Solid"
                                BorderWidth="1px" Font-Underline="False" HorizontalPadding="3px"
                                VerticalPadding="1px" />
                        </asp:TreeView>
                    </td>
                    <td>
			            Find below information on locked batches.<br/><br/>
                        <asp:Label ID="LabelCount" runat="server" Text="The record count is: "></asp:Label><br /><br />
                        <asp:SqlDataSource ID="sqlDataSource" runat="server"
                            SelectCommand="SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received))"
                            connectionString="<%$ ConnectionStrings:ModelFOTNIndexConnectionString %>"
                            onselected="SqlDataSource_Selected">
                            <SelectParameters>
                                <asp:Parameter DefaultValue="10" Name="CurrentEventTypeID" Type="Int32" />
                                <asp:Parameter DefaultValue="2" Name="Received" Type="Int32" />
                            </SelectParameters>
                        </asp:SqlDataSource>
                        <asp:GridView ID="GridView1" runat="server" AllowPaging="True"
                            AllowSorting="True" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None"
                            BorderWidth="1px" CellPadding="4" DataSourceID="SqlDataSource"
                            ForeColor="Black" GridLines="Vertical" PageSize="15" Visible="True"
                            onpageindexchanged="GridView1_PageIndexChanged" onsorted="GridView1_Sorted" >
                            <RowStyle BackColor="#F7F7DE" />
                            <EmptyDataTemplate>
                                No records were found.
                            </EmptyDataTemplate>
                            <FooterStyle BackColor="#CCCC99" />
                            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                    </td>
                </tr>
            </table>
        </div>
    </div>
    </form>
</body>
</html>


And, here is the code-behind:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Environments_Model_locked_batches : System.Web.UI.Page
{
		protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = IndexDataSource(sqlDataSource);
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "Choose a filter string");
        }
    }

    protected ArrayList IndexDataSource(SqlDataSource sqlDataSource)
    {
        ArrayList indexArrayList = new ArrayList();
        DataTable indexDataTable = new DataTable();
        DataView indexDataView = new DataView();

        indexDataView = (DataView)sqlDataSource.Select(DataSourceSelectArguments.Empty);
        indexDataTable = indexDataView.ToTable();

        foreach (DataRow dataRow in indexDataTable.Rows)
        {
            string[] rowIndex = (dataRow["UserBatchName"].ToString()).Split(' ');
            foreach (string word in rowIndex)
            {
                if (!indexArrayList.Contains(word))
                {
                    indexArrayList.Add(word);
                }
            }
        }

        indexArrayList.Sort();

        return indexArrayList;
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "Choose a filter string")
        {
            GridView1.Visible = true;
            GridView1.DataBind();
        }
        else
        {
            sqlDataSource.SelectParameters.Clear();
            sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
            sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
            GridView1.DataBind();
        }
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "Choose a filter string")
        {
            GridView1.Visible = true;
            GridView1.DataBind();
        }
        else
        {
            sqlDataSource.SelectParameters.Clear();
            sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
            sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
            GridView1.DataBind();
        }
    }

    protected void GridView1_Sorted(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedValue == "Choose a filter string")
        {
            GridView1.Visible = true;
            GridView1.DataBind();
        }
        else
        {
            sqlDataSource.SelectParameters.Clear();
            sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
            sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
            GridView1.DataBind();
        }
    }
    protected void SqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)
    {
        if (DropDownList1.SelectedValue == "Choose a filter string")
        {
            LabelCount.Text = "The record count for all records is: " + e.AffectedRows.ToString();
        }
        else
        {
            LabelCount.Text = "The record count for " + DropDownList1.SelectedValue + " is: " + e.AffectedRows.ToString();
        }

        if (e.Exception != null)
        {
            LabelCount.Visible = false;
            GridView1.EmptyDataText = e.Exception.Message;
        }
    }
}


When I try to run the page, I get this:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Compilation Error 
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. 

Compiler Error Message: CS0103: The name 'DropDownList1' does not exist in the current context

Source Error:

 

Line 15:         if (!IsPostBack)
Line 16:         {
Line 17:             DropDownList1.DataSource = IndexDataSource(sqlDataSource);
Line 18:             DropDownList1.DataBind();
Line 19:             DropDownList1.Items.Insert(0, "Choose a filter string");
 

Source File: c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs    Line: 17 



Show Detailed Compiler Output:


c:\windows\system32\inetsrv> "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\csc.exe" /t:library /utf8output /R:"C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll" /R:"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.WorkflowServices\3.5.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /R:"C:\WINDOWS\assembly\GAC_32\System.Web\2.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Mobile\2.0.0.0__b03f5f7f11d50a3a\System.Web.Mobile.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Runtime.Serialization\3.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.IdentityModel\3.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel.Web\3.5.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\WINDOWS\assembly\GAC_MSIL\System.Web.Services\2.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /out:"c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f3c5e0e5\97ac95d6\App_Web_locked_batches.aspx.cs.d4225d1c.bewrivva.dll" /debug- /optimize+ /w:4 /nowarn:1659;1699;1701  "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f3c5e0e5\97ac95d6\App_Web_locked_batches.aspx.cs.d4225d1c.bewrivva.0.cs" "c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\root\f3c5e0e5\97ac95d6\App_Web_locked_batches.aspx.cs.d4225d1c.bewrivva.1.cs"


Microsoft (R) Visual C# 2005 Compiler version 8.00.50727.3053
for Microsoft (R) Windows (R) 2005 Framework version 2.0.50727
Copyright (C) Microsoft Corporation 2001-2005. All rights reserved.

c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(17,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(17,56): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(18,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(19,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(51,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(53,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(54,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(58,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(59,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(59,65): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(60,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(61,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(67,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(69,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(70,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(74,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(75,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(75,65): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(76,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(77,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(83,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(85,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(86,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(90,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(91,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(91,65): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(92,13): error CS0103: The name 'sqlDataSource' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(93,13): error CS0103: The name 'GridView1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(98,13): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(100,13): error CS0103: The name 'LabelCount' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(104,13): error CS0103: The name 'LabelCount' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(104,57): error CS0103: The name 'DropDownList1' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(109,13): error CS0103: The name 'LabelCount' does not exist in the current context
c:\Inetpub\wwwroot\reference\Environments\Model\locked_batches.aspx.cs(110,13): error CS0103: The name 'GridView1' does not exist in the current context

 



Show Complete Compilation Source:


Line 1:    using System;
Line 2:    using System.Collections;
Line 3:    using System.Collections.Generic;
Line 4:    using System.Data;
Line 5:    using System.Data.SqlClient;
Line 6:    using System.Web;
Line 7:    using System.Web.UI;
Line 8:    using System.Web.UI.HtmlControls;
Line 9:    using System.Web.UI.WebControls;
Line 10:   
Line 11:   public partial class Environments_Model_locked_batches : System.Web.UI.Page
Line 12:   {
Line 13:   		protected void Page_Load(object sender, EventArgs e)
Line 14:       {
Line 15:           if (!IsPostBack)
Line 16:           {
Line 17:               DropDownList1.DataSource = IndexDataSource(sqlDataSource);
Line 18:               DropDownList1.DataBind();
Line 19:               DropDownList1.Items.Insert(0, "Choose a filter string");
Line 20:           }
Line 21:       }
Line 22:   
Line 23:       protected ArrayList IndexDataSource(SqlDataSource sqlDataSource)
Line 24:       {
Line 25:           ArrayList indexArrayList = new ArrayList();
Line 26:           DataTable indexDataTable = new DataTable();
Line 27:           DataView indexDataView = new DataView();
Line 28:   
Line 29:           indexDataView = (DataView)sqlDataSource.Select(DataSourceSelectArguments.Empty);
Line 30:           indexDataTable = indexDataView.ToTable();
Line 31:   
Line 32:           foreach (DataRow dataRow in indexDataTable.Rows)
Line 33:           {
Line 34:               string[] rowIndex = (dataRow["UserBatchName"].ToString()).Split(' ');
Line 35:               foreach (string word in rowIndex)
Line 36:               {
Line 37:                   if (!indexArrayList.Contains(word))
Line 38:                   {
Line 39:                       indexArrayList.Add(word);
Line 40:                   }
Line 41:               }
Line 42:           }
Line 43:   
Line 44:           indexArrayList.Sort();
Line 45:   
Line 46:           return indexArrayList;
Line 47:       }
Line 48:   
Line 49:       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
Line 50:       {
Line 51:           if (DropDownList1.SelectedValue == "Choose a filter string")
Line 52:           {
Line 53:               GridView1.Visible = true;
Line 54:               GridView1.DataBind();
Line 55:           }
Line 56:           else
Line 57:           {
Line 58:               sqlDataSource.SelectParameters.Clear();
Line 59:               sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
Line 60:               sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
Line 61:               GridView1.DataBind();
Line 62:           }
Line 63:       }
Line 64:   
Line 65:       protected void GridView1_PageIndexChanged(object sender, EventArgs e)
Line 66:       {
Line 67:           if (DropDownList1.SelectedValue == "Choose a filter string")
Line 68:           {
Line 69:               GridView1.Visible = true;
Line 70:               GridView1.DataBind();
Line 71:           }
Line 72:           else
Line 73:           {
Line 74:               sqlDataSource.SelectParameters.Clear();
Line 75:               sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
Line 76:               sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
Line 77:               GridView1.DataBind();
Line 78:           }
Line 79:       }
Line 80:   
Line 81:       protected void GridView1_Sorted(object sender, EventArgs e)
Line 82:       {
Line 83:           if (DropDownList1.SelectedValue == "Choose a filter string")
Line 84:           {
Line 85:               GridView1.Visible = true;
Line 86:               GridView1.DataBind();
Line 87:           }
Line 88:           else
Line 89:           {
Line 90:               sqlDataSource.SelectParameters.Clear();
Line 91:               sqlDataSource.SelectParameters.Add("UserBatchName", DropDownList1.SelectedValue);
Line 92:               sqlDataSource.SelectCommand = "SELECT [AppID], [BatchID], [UserBatchName], [LastEventDate], [Message], [LockedUserID] FROM [Batch] WHERE (([CurrentEventTypeID] = @CurrentEventTypeID) AND ([Received] = @Received) OR (UserBatchName LIKE '% ' + @UserBatchName) OR (UserBatchName LIKE @UserBatchName + ' %') OR (UserBatchName LIKE '% ' + @UserBatchName + ' %') OR (UserBatchName = @UserBatchName))";
Line 93:               GridView1.DataBind();
Line 94:           }
Line 95:       }
Line 96:       protected void SqlDataSource_Selected(object sender, SqlDataSourceStatusEventArgs e)
Line 97:       {
Line 98:           if (DropDownList1.SelectedValue == "Choose a filter string")
Line 99:           {
Line 100:              LabelCount.Text = "The record count for all records is: " + e.AffectedRows.ToString();
Line 101:          }
Line 102:          else
Line 103:          {
Line 104:              LabelCount.Text = "The record count for " + DropDownList1.SelectedValue + " is: " + e.AffectedRows.ToString();
Line 105:          }
Line 106:  
Line 107:          if (e.Exception != null)
Line 108:          {
Line 109:              LabelCount.Visible = false;
Line 110:              GridView1.EmptyDataText = e.Exception.Message;
Line 111:          }
Line 112:      }
Line 113:  }
 




--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.3615; ASP.NET Version:2.0.50727.3618 


Anywhere I have a control from the .aspx page, it shows the same error.  This is just the first control reference it encounters.

Will,

I must admit I'm pretty stumped.  Somehow, the .aspx and the code-behind aren't finding each other.

There's one thing I notice about your page that's different from mine.  In the <@Page> directive, you have a "Src=" attribute that my pages never have.  According to this MSDN article:

"You can use the Src attribute to link build providers to the page. For more information, see the BuildProvider class. Also, in versions of ASP.NET prior to 2.0, the Src attribute was used as an alternative way to link a code-behind file to a page. In ASP.NET 2.0, the preferred approach to linking a code-behind source file to a page is to use the Inherits attribute to specify a class, along with the CodeFile attribute to specify the path to the source file for the class."

That's something I've never needed, and I suggest that you try removing it.

I am beginning to this machine has some gremlins.  I use the Src attribute as a workaround to compiler issue.  It was saying it couldn't find the .cs file.  When I added the Src attribute, which I had never used either, it worked fine.  I am positive I have the site set to ASP.NET 2.0 in IIS, so, I don't think it's a version issue.  If I can't get it figured out soon, I might request a different box.  There are all sorts of weird things on this machine that make no sense.  I just wanted to be sure I wasn't overlooking something obvious.

Will,

Did you try taking the Src back out?

Your CodeFile attribute is where you should resolve the "couldn't find the .cs file" issue.  Based on what's in your <@Page> directive, your code must be in a file named "locked_batches.aspx.cs" which must be in the same directory as the .aspx.

Let me know.

 

AHA! I found another possible fix.

The same MSDN page says:

"[CodeFile] is used for Web site projects. The CodeBehind attribute is used for Web application projects."

In other words, if you are creating a Web Application (as opposed to a Web Site) you should change "CodeFile" to "CodeBehind" in your <@Page>.

[RESOLVED] Closing Form

I have the following code to open a new window withoug toolbars, etc.  The only problem is that it ask if I want to close the old form.  Also, the controls on the second form are adjusting size on some computers and doesn't look very good.  Any ideals on how to close the first form without prompt and how can I cange my code to look the same on every comptuer.  The first set of code is from code behind.  The second set is from my second form.

Mike

 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then

            Dim width, height, url, target, status, attributes As String

            width = "width=500"
            height = "height=500"
            target = "_new"
            url = "PhoneBook.aspx"
            status = "status=0"
            attributes = width & ", " & height & ", " & target & ", " & status

            Dim js As String = "<script>window.open('" & url & "','" & target & "', '" & attributes & "');window.close()</script>"
            ClientScript.RegisterStartupScript(GetType(Page), "Launch_New_Window", js)
        End If
    End Sub

 

 

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

<!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>City Phone Extension List</title>
    <script language="javascript" type="text/javascript">
        function maximize() {
          window.resizeTo(650,140);
          window.moveTo(0,0);  
        }
        maximize();
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblName" runat="server" Text="LastName, FirstName" style="top: 5px; left: 32px; position: absolute; height: 19px; width: 121px" Font-Bold="True" Font-Names="Tahoma" Font-Size="XX-Small"></asp:Label>
        <asp:Label ID="lblExtension" runat="server" Text="Extension" 
            style="top: 5px; left: 186px; position: absolute; height: 19px; width: 67px" 
            Font-Bold="True" Font-Names="Tahoma" Font-Size="XX-Small"></asp:Label>
        <asp:Label ID="lblDID" runat="server" Text="DID" 
            style="top: 5px; left: 325px; position: absolute; height: 19px; width: 62px" 
            Font-Bold="True" Font-Names="Tahoma" Font-Size="XX-Small"></asp:Label>
        <asp:DropDownList ID="Name" runat="server" 
            style="top: 25px; left: 10px; position: absolute; height: 25px; width: 145px; right: 1055px;" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="FullName" DataValueField="ID" Font-Bold="True" Font-Names="tahoma" Font-Size="X-Small"> </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PhoneCS %>" 
            SelectCommand="SELECT ID, RTRIM(LastName) + N', ' + FirstName AS FullName, Extension FROM Directory ORDER BY FullName"> </asp:SqlDataSource>
        <asp:DropDownList ID="Extension" runat="server" 
            style="top: 25px; position: absolute; height: 25px; width: 89px; left: 166px; right: 947px;" 
            DataSourceID="SqlDataSource2" DataTextField="Extension" DataValueField="ID" 
            Font-Bold="True" Font-Names="tahoma" Font-Size="X-Small" AutoPostBack="True"> </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PhoneCS %>" 
            SelectCommand="SELECT ID, Extension FROM Directory ORDER BY Extension"> </asp:SqlDataSource>
        <asp:DropDownList ID="DID" runat="server" 
            style="top: 25px; position: absolute; width: 141px; left: 266px" 
            DataSourceID="SqlDataSource3" DataTextField="DID1" DataValueField="ID" 
            Font-Bold="True" Font-Names="tahoma" Font-Size="X-Small" 
            AutoPostBack="True">  </asp:DropDownList>
    </div>
    <div>
    </div>
    <asp:Panel ID="Panel1" runat="server">
     <asp:Button ID="A" runat="server" Text="A" style="top: 70px; left: 10px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" Font-Size="X-Small" />
     <asp:Button ID="B" runat="server" Text="B" style="top: 70px; left: 30px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="C" runat="server" Text="C" style="top: 70px; left: 50px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="D" runat="server" Text="D" style="top: 70px; left: 70px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="E" runat="server" Text="E" style="top: 70px; left: 90px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="F" runat="server" Text="F" style="top: 70px; left: 110px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="G" runat="server" Text="G" style="top: 70px; left: 130px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="H" runat="server" Text="H" style="top: 70px; left: 150px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="I" runat="server" Text="I" style="top: 70px; left: 170px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="J" runat="server" Text="J" style="top: 70px; left: 190px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="K" runat="server" Text="K" style="top: 70px; left: 210px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="L" runat="server" Text="L" style="top: 70px; left: 230px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="M" runat="server" Text="M" style="top: 70px; left: 250px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="N" runat="server" Text="N" style="top: 70px; left: 270px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="O" runat="server" Text="O" style="top: 70px; left: 290px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="P" runat="server" Text="P" style="top: 70px; left: 310px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="Q" runat="server" Text="Q" style="top: 70px; left: 330px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="R" runat="server" Text="R" style="top: 70px; left: 350px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="S" runat="server" Text="S" style="top: 70px; left: 370px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="T" runat="server" Text="T" style="top: 70px; left: 390px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="U" runat="server" Text="U" style="top: 70px; left: 410px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="v" runat="server" Text="W" style="top: 70px; left: 430px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="W" runat="server" Text="W" style="top: 70px; left: 450px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="X" runat="server" Text="X" style="top: 70px; left: 470px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="Y" runat="server" Text="Y" style="top: 70px; left: 490px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="Z" runat="server" Text="Z" style="top: 70px; left: 510px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />
     <asp:Button ID="All" runat="server" Text="All" style="top: 70px; left: 530px; position: absolute; height: 26px; width: 21px" Font-Bold="True" Font-Names="Tahoma" />


    </asp:Panel>

    <p>
        &nbsp;</p>

        <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
            ConnectionString="<%$ ConnectionStrings:PhoneCS %>" 
            
        SelectCommand="SELECT ID, '(865) ' + SUBSTRING(DID, 1, 3) + '-' + SUBSTRING(DID, 4, 4) AS DID1 FROM Directory ORDER BY DID1"> </asp:SqlDataSource>

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

For Closing window without warning sign, try this....

http://blogs.x2line.com/al/articles/350.aspx

And about the styling of the controls...it is a CSS issue.....may be you got to play around with some CSS attributes like height, width etc...

Thanks,

[RESOLVED] populate DDL using data from database

I have a simple ASP.NET assignment to complete. We are supposed to create a simple database with one table. That table should contain records that contain a question and 3 possible choices. We need to populate a DDL with the 3 possible choices. But, as far as I can tell, you can only populate a DDL with a FIELD of values, not select values from one row. So, I'm confused.

Is it possible to get select values from a row of data in a databse using an AccessDataSource and populate a DDL with them?

Ideally, for this requirement table should be created like this:

 

And you can fill out the answer dropdownlist with this query like:

Select AnswerID, AnswerText from tblAnswer where QuestionID = @CurrentQuestionID

 

Hope it helps./.

This makes sense. Although the instructor required that we only use one table for all of the data including the question text and the 3 answers. I'll try your suggestion though. I'll be happy if I can get it to work like this. I'm not that familiar with this stuff yet but I think that your suggestion invloves using some "code-behind" to make this happen. The way I'm doiing it now is through the Data Source Wizard. So I'm not quite sure if I'll be able to figure this out but I'll give it a shot.

Using the Wizard you can only set the name and value properties to a FIELD. No matter what select statement I build I can only get values from a FIELD, not values from an individual record. Like, I want to get the 3 answers from one record and populate the DDL with them. I don't know if it's possible to do that.

 

Thanks!

Ok. You can do that as well.

The query will be a simple UNION ALL Query.

Check this out:

select AnswerText1 AS 'Answers' from tblQuestionAnswer
Where QuestionID = 1
union all
select AnswerText2 AS 'Answers' from tblQuestionAnswer
Where QuestionID = 1
union all
select AnswerText3 AS 'Answers' from tblQuestionAnswer
Where QuestionID = 1

I have used SqlDataSource instead of AccessDataSource here:

 

 

<asp:DropDownList ID="ddlAnswer" runat="server" DataSourceID="SqlDataSource1" DataTextField="Answers">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testdatabaseConnectionString %>"
    SelectCommand="select AnswerText1 AS 'Answers' from tblQuestionAnswer union all select AnswerText2 AS 'Answers' from tblQuestionAnswer union all select AnswerText3 AS 'Answers' from tblQuestionAnswer">
</asp:SqlDataSource>

 

 

hope it helps./.

Right on. That sounds more like what I need to do. I forgot about UNION ALL (just had a DB course last semester, so that stuff is slowly coming back to me now). I'll give this idea a shot. I'll report back one way or the other.

 

Thanks!

Thanks! Your UNION ALL solution worked. I had to change the code to work with an Access Data Source but it worked. I still have to tweak the select statement a little too so it grabs grab the exact data values from my DB that I need in the DDL but it's working.

 

Thanks again!