Click here to Skip to main content
15,940,122 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need help with my code. I have a drop down list box that is connected to a database that has the year in it. The drop down list should populate the textboxes I want but the textboxes are blank. What did I do wrong? Can someone check my code and let me know where I went wrong at. Thanks.

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
Comments
ZurdoDev 5-Jun-13 11:38am    
Have you checked your code? Just put a breakpoint at the top, walk through it, and see what happens.

1 solution

First issue seems to be in this section:
C#
SqlCommand sqlCmd = new SqlCommand("SELECT * from TableFIN WHERE FINYR = @FINYR", connection);
sqlCmd.Parameters.AddWithValue("@FINYR", user);

where
C#
user = this.User.Identity.Name;

You said you have dropdownlist that has years. So I assume you should be passing dropdownlist's value to your query. Like:
C#
sqlCmd.Parameters.AddWithValue("@FINYR", DropDownList1.SelectedValue);


When this code start working improve your query:
C#
SqlCommand scmd = new SqlCommand("Select FINYR from TableFIN where TOTASSETS, TOTLIABILITY and NONEXPPERMRESASSETS = " + DropDownList1.SelectedValue.ToString() + "@TOTASSETS, @TOTLIABILITY and @NONEXPPERMRESASSETS", con);


This is so very wrong. If you specify what should it return, I can help you further.
 
Share this answer
 
v2
Comments
Computer Wiz99 5-Jun-13 11:59am    
Zafar Sultan, It should return numbers that are in the database. I have a database with financial info and with the drop down list that has years in it like 1999, 200, 2001 and 2002 it should populate the textboxes that I called for those fields.
Zafar Sultan 5-Jun-13 12:01pm    
You should again fire the same query because your data depends upon year: SqlCommand sqlCmd = new SqlCommand("SELECT * from TableFIN WHERE FINYR = @FINYR", connection); sqlCmd.Parameters.AddWithValue("@FINYR", DropDownList1.SelectedValue);
Zafar Sultan 5-Jun-13 12:03pm    
Updated my comment.
Computer Wiz99 5-Jun-13 12:32pm    
Ok. Thanks. I will give it a go.

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