Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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
        {

        }
    }
}
Posted

1 solution

C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["PassConnectionString"].ConnectionString);

SqlCommand scmd = new SqlCommand("Select FINYR,TOTASSETS ,TOTLIABILITY,NONEXPPERMRESASSETS from TableFIN where  NONEXPPERMRESASSETS = " + DropDownList1.SelectedValue.ToString(), con);
con.Open();
SqlDataReader dr = scmd.ExecuteReader();
if(dr.Read())
{
            TextBoxLYTA.Text = dr["TOTASSETS"].ToString();
 
            TextBoxLYTL.Text = dr["TOTLIABILITY"].ToString();
 
            TextBoxLYNPRNA.Text = dr["NONEXPPERMRESASSETS"].ToString();

}
dr.Close();
con.Close();
}
 
Share this answer
 
v2
Comments
Computer Wiz99 27-Jun-13 9:26am    
Ankit Kul,

Thanks I will give it a try.
Asp_Learner 27-Jun-13 9:40am    
If you still have any problem ,please let me know here.
Computer Wiz99 27-Jun-13 10:02am    
Ok. I will. Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900