C# 使用SQL数据库中的字符串选择DropDownList值
我无法使用下面的代码将DropDownList设置为正确的值C# 使用SQL数据库中的字符串选择DropDownList值,c#,asp.net,sql-server,database,C#,Asp.net,Sql Server,Database,我无法使用下面的代码将DropDownList设置为正确的值 private DataTable loadAdBusinessTypes1() { string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString(); SqlConnection oSqlConnection = new SqlCo
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "pGetAdBusinessTypes";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
return oDataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
if (!Page.IsPostBack)
{
DataTable oDataTable = loadAdBusinessTypes1();
BusTypeddl.DataSource = oDataTable;
BusTypeddl.DataTextField = "business_type";
BusTypeddl.DataBind();
}
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
oSqlConnection.Open();
oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
{
if (reader1.Read())
{
var findBusType = reader1["business_type"].ToString().Trim();
var selectedIndex = -1;
for (int i = 0; i < BusTypeddl.Items.Count; i++)
{
if (BusTypeddl.Items[i].ToString() == findBusType)
{
selectedIndex = i;
break;
}
}
if (selectedIndex > -1)
{
BusTypeddl.SelectedIndex = selectedIndex;
}
}
}
}
}
else语句中的代码对我不起作用,但它也不会抛出任何异常
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "pGetAdBusinessTypes";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
return oDataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
if (!Page.IsPostBack)
{
DataTable oDataTable = loadAdBusinessTypes1();
BusTypeddl.DataSource = oDataTable;
BusTypeddl.DataTextField = "business_type";
BusTypeddl.DataBind();
}
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
oSqlConnection.Open();
oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
{
if (reader1.Read())
{
var findBusType = reader1["business_type"].ToString().Trim();
var selectedIndex = -1;
for (int i = 0; i < BusTypeddl.Items.Count; i++)
{
if (BusTypeddl.Items[i].ToString() == findBusType)
{
selectedIndex = i;
break;
}
}
if (selectedIndex > -1)
{
BusTypeddl.SelectedIndex = selectedIndex;
}
}
}
}
}
DropDownList是从我存储程序信息的一个数据库中设置的。我用reader1访问的数据库是一个用户信息数据库,它的字符串值是在注册时从DropDownList中选择的,因此我不能使用索引。有人能指出我哪里出了问题吗
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "pGetAdBusinessTypes";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
return oDataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
if (!Page.IsPostBack)
{
DataTable oDataTable = loadAdBusinessTypes1();
BusTypeddl.DataSource = oDataTable;
BusTypeddl.DataTextField = "business_type";
BusTypeddl.DataBind();
}
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
oSqlConnection.Open();
oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
{
if (reader1.Read())
{
var findBusType = reader1["business_type"].ToString().Trim();
var selectedIndex = -1;
for (int i = 0; i < BusTypeddl.Items.Count; i++)
{
if (BusTypeddl.Items[i].ToString() == findBusType)
{
selectedIndex = i;
break;
}
}
if (selectedIndex > -1)
{
BusTypeddl.SelectedIndex = selectedIndex;
}
}
}
}
}
我只想从用户数据库检索文本字符串,从程序数据库填充DropDownList,然后让数据库显示与字符串匹配的项。这是最后一部分,自动选择项目,这就是问题所在
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "pGetAdBusinessTypes";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
return oDataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
if (!Page.IsPostBack)
{
DataTable oDataTable = loadAdBusinessTypes1();
BusTypeddl.DataSource = oDataTable;
BusTypeddl.DataTextField = "business_type";
BusTypeddl.DataBind();
}
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
oSqlConnection.Open();
oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
{
if (reader1.Read())
{
var findBusType = reader1["business_type"].ToString().Trim();
var selectedIndex = -1;
for (int i = 0; i < BusTypeddl.Items.Count; i++)
{
if (BusTypeddl.Items[i].ToString() == findBusType)
{
selectedIndex = i;
break;
}
}
if (selectedIndex > -1)
{
BusTypeddl.SelectedIndex = selectedIndex;
}
}
}
}
}
尝试将第一种方法更改为:
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand();
oSqlCommand.Connection = oSqlConnection;
oSqlCommand.CommandType = CommandType.StoredProcedure;
oSqlCommand.CommandText = "pGetAdBusinessTypes";
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter();
oSqlDataAdapter.SelectCommand = oSqlCommand;
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
return oDataTable1;
}
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
if (!Page.IsPostBack)
{
DataTable oDataTable = loadAdBusinessTypes1();
BusTypeddl.DataSource = oDataTable;
BusTypeddl.DataTextField = "business_type";
BusTypeddl.DataBind();
}
try
{
if (Session["New"] == null)
{
Response.Redirect("~/Account/Login.aspx"); //*****CHANGE REDIRECT WEBPAGE*****
}
else
{
string str = Convert.ToString(Session["New"]);
string cmdText = @"select account_no, first_name, last_name, email_1, email_2, business_street_1, business_street_2, business_street_3, business_city, business_state, business_postal_code, business_country, company_name, business_type, phone_1, phone_2, phonecode_1, phonecode_2, website, registration_date, screens_no, user_password from users where email_1 =@email";
string cpUsersConnection = ConfigurationManager.ConnectionStrings["cp_usersConnection"].ToString();
using (SqlConnection oSqlConnection = new SqlConnection(cpUsersConnection))
using (SqlCommand oSqlCommand = new SqlCommand(cmdText, oSqlConnection))
{
oSqlConnection.Open();
oSqlCommand.Parameters.Add("@email", SqlDbType.NVarChar).Value = str;
using (SqlDataReader reader1 = oSqlCommand.ExecuteReader())
{
if (reader1.Read())
{
var findBusType = reader1["business_type"].ToString().Trim();
var selectedIndex = -1;
for (int i = 0; i < BusTypeddl.Items.Count; i++)
{
if (BusTypeddl.Items[i].ToString() == findBusType)
{
selectedIndex = i;
break;
}
}
if (selectedIndex > -1)
{
BusTypeddl.SelectedIndex = selectedIndex;
}
}
}
}
}
private DataTable loadAdBusinessTypes1()
{
string prgInfoConnection = ConfigurationManager.ConnectionStrings["program_infoConnection"].ToString();
SqlConnection oSqlConnection = new SqlConnection(prgInfoConnection);
SqlCommand oSqlCommand = new SqlCommand("pGetAdBusinessTypes", oSqlConnection);
oSqlCommand.CommandType = CommandType.StoredProcedure;
DataTable dt = new DataTable();
try{
oSqlConnection.Open();
SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(oSqlCommand);
DataTable oDataTable1 = new DataTable("AdBusinessTypes");
oSqlDataAdapter.Fill(oDataTable1);
dt = oDataTable1;
}
catch(Exception ex){
//you may work with exceptions here
}
finally{
oSqlConnection.Close();
}
return dt;
}
findBusType列包含一个字符串,您需要在Bustypedll中搜索该字符串。如果找到,则将其设置为选中,否则将选择默认值?这就是你要找的吗?是的。findBusType只是我用来将reader1在列中找到的正确值设置为的变量。它是ASP.NET,因此项是ListItems,ListItem具有属性Text,如果Bustypedll.items[i].Text.Trim==FindBustypedDoesn恐怕也不起作用,请尝试使用。它已执行,但ddl仍显示其默认选择。@DMur将AutoPostBack属性添加到DropDownList控件,在其上填充OnSelectedIndexChanged,并在OnSelectedIndexChanged事件方法中绑定文本框的值