3 Replies - 2778 Views - Last Post: 28 September 2009 - 06:52 AM Rate Topic: -----

#1 syanas  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-September 09

drop down list

Posted 28 September 2009 - 12:40 AM


<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master" CodeFile="CourseReg.aspx.cs" Inherits="CourseReg" Title="Course" %>




<asp:Content ID="Content1" runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
	<asp:FormView ID="FormView1" runat="server"  
		DataKeyNames="StudID" 
		DataSourceID="SqlDataSource1" Width="238px">
		
		<ItemTemplate>
			<asp:Label ID="lblID" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="Student ID :"></asp:Label>
					<asp:TextBox ID="txtID" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="70px" 
						Text='<%#Eval("StudID")%>'></asp:TextBox>
			<br />
			
			<asp:Label ID="lblName" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="Name :"></asp:Label>
					<asp:TextBox ID="txtName" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="131px" 
						Text='<%#Bind("StudName")%>'></asp:TextBox>
				<br />
			<asp:Label ID="lblIC" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="IC No :"></asp:Label>
					<asp:TextBox ID="txtIC" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="97px" 
						Text='<%#Bind("StudIC")%>'></asp:TextBox>
				<br />
			<asp:Label ID="lblBirth" runat="server" 
				BorderStyle="None" 
				Width="86px" 
				Text="Date of Birth :"></asp:Label>
					<asp:TextBox ID="txtBirth" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="97px" 
						Text='<%#Bind("DateOfBirth")%>'></asp:TextBox>
				<br />
			<asp:Label ID="lblSex" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="Sex :"></asp:Label>
					<asp:TextBox ID="txtSex" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="32px" 
						Text='<%#Bind("Sex")%>'></asp:TextBox>
				<br />
			<asp:Label ID="lblNationality" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="Nationality :"></asp:Label>
					<asp:TextBox ID="txtNationality" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="96px" 
						Text='<%#Bind("Nationality")%>'></asp:TextBox>
				<br/>
			<asp:Label ID="lblAdd" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				Text="Address :"></asp:Label>
					<asp:TextBox ID="txtAdd" runat="server" 
						BackColor="Aqua" 
						ReadOnly="true" 
						Width="213px" 
						Height="100px" 
						TextMode="MultiLine"  
						Text='<%#Bind("Address")%>'></asp:TextBox>
			<br />
		</ItemTemplate>  
  
	</asp:FormView>
	
	<asp:SqlDataSource ID = "SqlDataSource1" runat = "server" 
			ConnectionString="<%$ConnectionStrings:ConnectionString %>" 
			SelectCommand="select [StudID],
								  [StudName],
								  [StudIC],
								  [DateOfBirth],
								  [Sex],
								  [Nationality],
								  [Address] 
							 from [Student] 
							 order by [StudID]">
	</asp:SqlDataSource>  
</asp:Content>

<%--select--%>

<asp:Content ID="Content2" runat="server" ContentPlaceHolderID="ContentPlaceHolder2">
<br />
	
		 <br />
			<strong><span style="font-size: 14pt;"><em><span style="color: #003300"><span style="font-family: Bell MT">
			Select the course category below to add a new course : </span>
			<br />
			</span>
				</em></span></strong>
			<br />
	
		   
			<asp:Label ID="Label1" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				ForeColor="Black"></asp:Label>
				
			<asp:DropDownList ID="ddl1" runat="server" 
				AutoPostBack="false" 
				DataTextField="CourseName" 
				DataValueField="CourseID"
				DataSourceID="SqlDataSource3" Width="400px" ForeColor="#000000" >
				
			   <asp:ListItem >--Select--</asp:ListItem>

			</asp:DropDownList>
			
				   
			<asp:Button ID="btnAdd" runat="server" 
				onclick="btnAdd_Click" 
				Text="Add Course" 
				BackColor="Lime" 
				ForeColor="DarkSlateGray" />
			<br />
			
			
			<asp:Label ID="Label2" runat="server" 
				BorderStyle="None" 
				Width="80px" 
				ForeColor="Black"></asp:Label>
				
			<asp:DropDownList ID="ddl2" runat="server" 
				AutoPostBack="false" 
				DataTextField="CourseName" 
				DataValueField="CourseID"
				DataSourceID="SqlDataSource4" Width="400px" ForeColor="#000000" >
				
			   <asp:ListItem >--Select--</asp:ListItem>

			</asp:DropDownList>
			
			<asp:Button ID="btnA" runat="server" 
				onclick="btnA_Click" 
				Text="Add ECourse" 
				BackColor="Lime" 
				ForeColor="DarkSlateGray" />
			<br />
			
	   
	
	<asp:SqlDataSource ID="SqlDataSource3" runat="server"
			ConnectionString="<% $ConnectionStrings:ConnectionString %>" 
			SelectCommand = "select [CourseID],
									[CourseName],
									[CreditHour]
							   from [Course1] 
							   order by [CourseID]">					   
	</asp:SqlDataSource>
	
	<asp:SqlDataSource ID="SqlDataSource4" runat="server"
			ConnectionString="<% $ConnectionStrings:ConnectionString %>" 
			SelectCommand = "select [CourseID],
									[CourseName],
									[CreditHour]
							   from [ECourse1] 
							   order by [CourseID]">					   
	</asp:SqlDataSource>
	
	
	<br />
 
 
<%--	
 <table> --%>
 
 
   
 <table border="0" width="750">
	<tr>
		<td>
		
			<asp:GridView ID="GridView1" runat="server" 
			AutoGenerateColumns="false" 
			DataKeyNames="CourseID" 
			DataSourceID="SqlDataSource2" 
			onrowdeleted="GridView1_RowDeleted" 
			OnRowUpdated="GridView1_RowUpdated" 
			onselectedIndexChanged="GridView1_SelectedIndexChanged" >
			
				<Columns>
					<asp:BoundField DataField="CourseID" 
									HeaderText="Course ID" 
									ReadOnly="True" >
									<HeaderStyle HorizontalAlign="Center" />
									<ItemStyle Width="80px"/>
					</asp:BoundField>
					
					<asp:BoundField DataField="CourseName" 
									HeaderText="Course Name" >
									<HeaderStyle HorizontalAlign="Center" />
									<ItemStyle Width="300px"/>
					</asp:BoundField>
					
					<asp:BoundField DataField="CreditHour" 
									HeaderText="Credit Hour">
									<HeaderStyle HorizontalAlign="Center" />
									<ItemStyle Width="80px"/>
					</asp:BoundField>
						<asp:CommandField CausesValidation="False"  
										  ShowDeleteButton="True" 
										  DeleteText="Drop" />										   
			  </Columns>
		<AlternatingRowStyle BorderStyle="Dashed" BackColor="Aqua" />
	</asp:GridView>
	
	<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
			ConflictDetection="CompareAllValues" 
			ConnectionString="<% $ConnectionStrings:ConnectionString %>" 
			OldValuesParameterFormatString="original_{0}" 
			DeleteCommand = "Delete from [Course] 
							 where [CourseID] = @original_CourseID
							 and [CourseName] = @original_CourseName
							 and [CreditHour] = @original_CreditHour
							 " 
			InsertCommand = "insert into [Course] ([CourseID],[CourseName],[CreditHour])
							 values (@CourseID,@CourseName,@CreditHour)" 
			SelectCommand = "select [CourseID],[CourseName],[CreditHour] 
							 from [Course] 
							 order by [CourseID]"
			UpdateCommand = "update [Course] 
							 set [CourseName] = @CourseName, 
							 [CreditHour] = @CreditHour 
							 where [CourseID] = @original_CourseID
							 and [CourseName] = @original_CourseName
							 and [CreditHour] = @original_CreditHour
							 ">
							 
							 <DeleteParameters>
								<asp:Parameter Name = "original_CourseID" Type="string" />
								<asp:Parameter Name = "original_CourseName" Type="string" />
								<asp:Parameter Name = "original_CreditHour" Type="string" />
								
							 </DeleteParameters>
							 
							 <UpdateParameters>
								<asp:Parameter Name = "CourseName" Type="string" />
								<asp:Parameter Name = "CreditHour" Type="string" />
								
								<asp:Parameter Name = "original_CourseID" Type="string" />
								<asp:Parameter Name = "original_CourseName" Type="string" />
								<asp:Parameter Name = "original_CreditHour" Type="string" />
								
							 </UpdateParameters>
							 
							 <InsertParameters>
								<asp:Parameter Name = "CourseID" Type="string" />
								<asp:Parameter Name = "CourseName" Type="string" />
								<asp:Parameter Name = "CreditHour" Type="string" />
							   
							 </InsertParameters>
							 
	  </asp:SqlDataSource>
	<br />
		 <asp:Label ID="lblMessage" runat="server" EnableViewState="false" ForeColor="red"></asp:Label>
		 <br />
		
	 </td>  
	</tr>
   
	
	
	
	</table>
	
		
	
</asp:Content>






---code behind-------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class CourseReg : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{

	}

	protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
	{

	}

	protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
	{
		lblMessage.Text = "Course Drop";
	}

	protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs  e)
	{
		if (e.Exception != null)
		{
			lblMessage.Text = "Incorrect data. Please try again.";
			e.ExceptionHandled = true;
			e.KeepInEditMode = true;
		}
		else if (e.AffectedRows == 0)
		{
			lblMessage.Text = "That category could not be updated. please try again.";
		}
	}


	protected void btnAdd_Click(object sender, EventArgs e)
	{
		setParameter("CourseID", ddl1.SelectedValue.ToString());
		setParameter("CourseName", ddl1.SelectedItem.Text.ToString());
		setParameter("CreditHour", ddl1.SelectedItem.Text.ToString());
	  

		try
		{
			SqlDataSource2.Insert();
			//ddl1.Text = "";
			//ddl1.SelectedValue = "";
			//ddl1.Text = "";
			//ddl1.Text = "";
			//ddl1.SelectedItem.Text.ToString();
			//ddl1.SelectedValue.ToString();
			//ddl1.SelectedItem.Text.ToString();

			//lblMessage.Text = "add " + ddl1.SelectedItem.Text.ToString();
		}
		catch (Exception)
		{
			lblMessage.Text = "There is already a category with that ID. Please try another,";
		}

		
	}

	private void setParameter(string CourseName, string value)
	{
		SqlDataSource2.InsertParameters[CourseName].DefaultValue = value;
		//SqlDataSource3.InsertParameters[CourseName].DefaultValue = value;
	}

	protected void btnA_Click(object sender, EventArgs e)
	{
		setParameter("CourseID", ddl1.SelectedValue.ToString());
		setParameter("CourseName", ddl1.SelectedItem.Text.ToString());
		setParameter("CreditHour", ddl1.SelectedItem.ToString());


		try
		{
			SqlDataSource2.Insert();
			//ddl1.Text = "";
			//ddl1.SelectedValue = "";
			//ddl1.Text = "";
			//ddl1.Text = "";
			//ddl1.SelectedItem.Text.ToString();
			//ddl1.SelectedValue.ToString();
			//ddl1.SelectedItem.Text.ToString();

			//lblMessage.Text = "add " + ddl1.SelectedItem.Text.ToString();
		}
		catch (Exception)
		{
			lblMessage.Text = "There is already a category with that ID. Please try another,";
		}


	}
}



Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)

Attached image(s)

  • Attached Image
  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: drop down list

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: drop down list

Posted 28 September 2009 - 05:27 AM

so is there a question/problem?
Was This Post Helpful? 0
  • +
  • -

#3 Elcric  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 101
  • View blog
  • Posts: 453
  • Joined: 02-May 09

Re: drop down list

Posted 28 September 2009 - 06:03 AM

Hello, :D

Also include some information on the purpose of the program.
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: drop down list

Posted 28 September 2009 - 06:52 AM

First moved to ASP.NET, Second:

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:

  • Post the code you're having problems with (DONE)
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1