如何检索列表框的值.valuemember财产
本文关键字:valuemember 财产 列表 何检索 检索 | 更新日期: 2023-09-27 17:49:27
我是c#编程初学者。我在这个模式下过滤结果:
listamicizie1.DisplayMember = "viewname"; // it's a alias
listamicizie1.ValueMember = "IdUtente"; //it's a primary key in my db table
listamicizie1.DataSource = ds.Tables["UTENTI"]; //db table
现在我必须在另一个db表中注册listbox结果的值,我使用该代码
string idu = string.Empty;
string value = string.Empty;
foreach (var item in listamicizie1.ValueMember)
{
for (int j = 0; j < listamicizie1.Items.Count; j++)
{
idu = listamicizie1.SelectedValue;
value += idu + ",";
}
}
value = value.TrimEnd(',');
value = value.TrimEnd(',');
idutente = user.Text;
string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["CnnStr"].ToString();
string querysql2 = " INSERT INTO AMICIZIA (IdUtente1, [idamici]) VALUES ('" + idutente + "', '" + value + "')";
SqlConnection myconn = new SqlConnection(CnnStr);
SqlCommand mycmd = new SqlCommand(querysql2, myconn);
try
{
myconn.Open();
mycmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myconn.Close();
}
}
this.Close();
}
程序正在运行,但是当我打开第二个数据库表(AMICIZIA)时,idamici中的值不是主键(第一个表中的数据),而是I,d,u,t,e,n,t,e,.
如何检索idutente的原始值?由于
(对不起,如果我写了一些错误,但我不是英语:))
不要用value.TrimEnd(',');
,用value.Replace(',', '');
也是使用Parameters
代替连接字符串的好做法。
string idu = string.Empty;
string value = string.Empty;
foreach (var item in listamicizie1.ValueMember)
{
for (int j = 0; j < listamicizie1.Items.Count; j++)
{
idu = listamicizie1.SelectedValue;
value += idu + ",";
}
}
//value = value.TrimEnd(',');
//value = value.TrimEnd(',');
value = value.Replace(',', '');
value = value.Replace(' ', ''); //Add this line if you do not want space.
idutente = user.Text;
string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["CnnStr"].ToString();
string querysql2 = " INSERT INTO AMICIZIA (IdUtente1, idamici) VALUES ('" + @idutente, @value)";
SqlConnection myconn = new SqlConnection(CnnStr);
SqlCommand mycmd = new SqlCommand(querysql2, myconn);
try
{
myconn.Open();
mycmd.Parameters.AddWithValue("@idutente", idutente);
mycmd.Parameters.AddWithValue("@value", value);
mycmd.ExecuteNonQuery();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
myconn.Close();
}
}
this.Close();
}
补充道:
您也可以在生成value
时更改代码value += idu + ",";
,参见代码
foreach (var item in listamicizie1.ValueMember)
{
for (int j = 0; j < listamicizie1.Items.Count; j++)
{
idu = listamicizie1.SelectedValue;
value += idu.Trim(idu);
}
}
//value = value.TrimEnd(',');
//value = value.TrimEnd(',');
现在不需要调用Replace
添加所有项目从列表框* *
试试这段代码。
string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["CnnStr"].ToString();
string querysql2 = " INSERT INTO AMICIZIA (IdUtente1, idamici) VALUES ('" + @idutente, @value)";
SqlConnection myconn = new SqlConnection(CnnStr);
SqlCommand mycmd = new SqlCommand(querysql2, myconn);
myconn.Open();
for (int i=0; i < listamicizie1.Items.Count; i++)
{
var myitem = listamicizie1.Items[i];
var dataArray = ( myitem as DataRowView).Row.ItemArray;
//MessageBox.Show(dataArray[0].ToString());
idutente = user.Text; //I don't know what are you storing here.
value = dataArray[0].ToString(); //dataArray[0] should have key
try
{
mycmd.Parameters.AddWithValue("@idutente", idutente);
mycmd.Parameters.AddWithValue("@value", value);
mycmd.ExecuteNonQuery();
mycmd.Parameters.Clear();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return;
}
finally
{
myconn.Close();
}
}