Click here to Skip to main content
16,015,531 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I get a drop down list box to populate textboxes in asp.net. I have some code but I am stuck. Please help!!

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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    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);

//this is what it needs to look like. just replace your values you want where the labels are
      SqlCommand scmd = new SqlCommand("Select FINYR from TableFIN where TOTASSETS = '" + [add value here] + "' and TOTLIABILITY = '" + [add value here] + "' and NONEXPPERMRESASSETS = '" + [add value here] + "'", con);

        

        try
        {

            con.Open();

            SqlDataReader sdr = scmd.ExecuteReader();

            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
Updated 4-Jun-13 9:58am
v8
Comments
Prasad Khandekar 4-Jun-13 10:04am    
Where are you stuck. What's the actual problem/errors you are facing?
Computer Wiz99 4-Jun-13 10:27am    
Well, first does my code look right. I know I have to finish filling some parts in.
Prasad Khandekar 4-Jun-13 10:33am    
You select SQL where clause is definitely wrong (Missing Critera). It should be something like

SELECT * FROM TableFIN WHERE birth_year = @birth_year

Then use sqlcmd.Parameters.AddWithValue("@birth_year", LYEAR.SelectedItem.Value);
Richard C Bishop 4-Jun-13 10:14am    
Where does it fail when you are debugging?
Computer Wiz99 4-Jun-13 10:27am    
Well, first does my code look right. I know I have to finish filling some parts in.

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