I am currently doing a college project using asp and c#. I have the sql database connected and I am able to write to it but i cant read from the database the way I want.
So I have a dropdown list populated by dates from my database and I want to populate text boxes relating to the selected date.
I have attached an image of my GUI to give you a better understanding and I have put the code below.
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Configuration;
using System.Data.Common;
using System.Data.SqlClient;
public partial class Staff_Home : System.Web.UI.Page
{
string sun1;
string sun2;
string WeekEnd;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
WeekEnd = (DropDownList1.Text);
AssignValues();
TextBox1.Text = sun1;
}
private void AssignValues()
{
SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
//Execute SQL Statement
SqlCommand comm = new SqlCommand("SELECT * FROM Schedule1 WHERE [WeekEnd] = @[WeekEnd]", conn);
//Set Parameters
// comm.Parameters.Add("@[WeekEnd]", SqlDbType.Int);
comm.Parameters.Add("@[WeekEnd]", SqlDbType.Text);
comm.Parameters["@[WeekEnd]"].Value = WeekEnd;
//Open Connection
conn.Open();
//Start SQL Reader
SqlDataReader reader = comm.ExecuteReader();
//SqlDataReader reader = comm.BeginExecuteReader();
while (reader.Read())
{
sun1 = (string)reader["sun1"];
sun2 = (string)reader["sun2"];
}
//Close Reader
reader.Close();
//Close Connection
conn.Close();
}
protected void SqlDataSource2_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
}
}