如何检索listbox.valuemember属性的值

时间:2014-03-10 09:55:47

标签: c# listbox

我是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();

}

程序正在运行,但是当我打开第二个db表(AMICIZIA)时,idamici中的值不是主键(第一个表中的idutente),而是i,d,u,t,e,n ,T,E,

如何追溯idutente的原始价值? 感谢

(对不起,如果我写了一些错误,但我不是英语:))

1 个答案:

答案 0 :(得分:0)

请勿使用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 += idu + ",";时更改代码value,请参阅代码

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

        }