下拉列表填充文本框帮助!!!! [英] Drop Down List Populate textbox Help!!!!
问题描述
我的代码需要帮助。我有一个下拉列表框连接到具有年份的数据库。下拉列表应该填充我想要的文本框,但文本框是空白的。我做错了什么?有人可以检查我的代码,让我知道我哪里出错了。谢谢。
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.
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
{
}
}
}
推荐答案
第一个问题似乎在本节中:
First issue seems to be in this section:
SqlCommand sqlCmd = new SqlCommand("SELECT * from TableFIN WHERE FINYR = @FINYR", connection);
sqlCmd.Parameters.AddWithValue("@FINYR", user);
其中
where
user = this.User.Identity.Name;
你说你有多年的下拉列表。所以我假设您应该将dropdownlist的值传递给您的查询。喜欢:
You said you have dropdownlist that has years. So I assume you should be passing dropdownlist's value to your query. Like:
sqlCmd.Parameters.AddWithValue("@FINYR", DropDownList1.SelectedValue);
当此代码开始工作时改进您的查询:
When this code start working improve your query:
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.
这篇关于下拉列表填充文本框帮助!!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!