Hello. I am trying to get data from my database to populate textboxes when you select a year from the drop down list. I can seem to get it to work. I am stuck. Can someone help with this populating to textboxes from drop box list selection.
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Configuration;
public partial class FinanccialIndicatorsForm : System.Web.UI.Page
{
private void getData(string user)
{
DataTable dt = new DataTable();
SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
connection.Open();
SqlCommand sqlCmd = new SqlCommand("SELECT * from TableFIN WHERE FINYR = @FINYR", connection);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.Parameters.AddWithValue("@FINYR", user);
sqlDa.Fill(dt);
if (dt.Rows.Count > 0)
{
TextBoxLYTA.Text = dt.Rows[0]["TOTASSETS"].ToString();
TextBoxLYTL.Text = dt.Rows[0]["TOTLIABILITY"].ToString();
TextBoxLYNPRNA.Text = dt.Rows[0]["NONEXPPERMRESASSETS"].ToString();
}
connection.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
getData(this.User.Identity.Name);
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
SqlCommand scmd = new SqlCommand("Select FINYR from TableFIN where TOTASSETS, TOTLIABILITY and NONEXPPERMRESASSETS = " + DropDownList1.SelectedValue.ToString() + "@TOTASSETS, @TOTLIABILITY and @NONEXPPERMRESASSETS", con);
try
{
con.Open();
SqlDataReader sdr = scmd.ExecuteReader();
sdr.Read();
TextBoxLYTA.Text = sdr["TOTASSETS"].ToString();
TextBoxLYTL.Text = sdr["TOTLIABILITY"].ToString();
TextBoxLYNPRNA.Text = sdr["NONEXPPERMRESASSETS"].ToString();
}
catch (Exception ex)
{
}
finally
{
con.Close();
}
}
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);
con.Open();
string insCmd = "Insert into TableFIN (TOTASSETS, TOTLIABILITY, NoNEXPPERMRESASSETS, UNRNETASSETS, TOTALREV, TUITFEES, CURRDEBT, LOMGTERMDEBT) values (@TOTASSETS, @TOTLIABILITY, @NoNEXPPERMRESASSETS, @UNRNETASSETS, @TOTALREV, @TUITFEES, @CURRDEBT, @LOMGTERMDEBT)";
SqlCommand insertUser = new SqlCommand(insCmd, con);
insertUser.Parameters.AddWithValue("@TOTASSETS", TextBoxTA.Text);
insertUser.Parameters.AddWithValue("@TOTLIABILITY", TextBoxTL.Text);
insertUser.Parameters.AddWithValue("@NoNEXPPERMRESASSETS", TextBoxNPRNA.Text);
insertUser.Parameters.AddWithValue("@UNRNETASSETS", TextBoxTUNA.Text);
insertUser.Parameters.AddWithValue("@TOTALREV", TextBoxTR.Text);
insertUser.Parameters.AddWithValue("@TUITFEES", TextBoxTFN.Text);
insertUser.Parameters.AddWithValue("@CURRDEBT", TextBoxCD.Text);
insertUser.Parameters.AddWithValue("@LOMGTERMDEBT", TextBoxLTD.Text);
try
{
insertUser.ExecuteNonQuery();
con.Close();
Response.Redirect(".aspx");
}
catch (Exception er)
{
Response.Write("You Have Successfully Submitted the Information!!!");
}
finally
{
}
}
}