0

我有一个用于申请编号的组合框。在从组合框中选择申请编号时,我们应该得到申请该特定申请的成本中心。这是代码:

        cmd.Connection = con;
        if (con.State != ConnectionState.Open)
        {
            con.Open();
        }
        txtcc.Text = "";
        if (cmbreqno.SelectedValue != null || Convert.ToString(cmbreqno.SelectedValue) != "")
        {
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "sp_displaycostcenter";
            cmd.Parameters.AddWithValue("@req_no", cmbreqno.SelectedValue);

            SqlDataReader rdr = cmd.ExecuteReader();


            adp.Fill(dsreqno,"selectedpurchase");
            if (dsreqno.Tables["selectedpurchase"].Rows.Count > 0)
            {
                txtcc.Text = dsreqno.Tables["selectedpurchase"].Rows[0]["req_no"].ToString();
            }
            dsreqno.Tables["selectedpurchase"].Clear();

        }
        cmd.Parameters.Clear();
        //rdr.Close();
        con.Close();
4

2 回答 2

0

尝试这个:

    if (con.State != ConnectionState.Open)
    {
        con.Open();
    }
    txtcc.Text = "";
    if (cmbreqno.SelectedValue != null || Convert.ToString(cmbreqno.SelectedValue) != "")
    {
        cmd.Connection = con;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "sp_displaycostcenter";
        cmd.Parameters.AddWithValue("@req_no", cmbreqno.SelectedValue);

        SqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
         txtcc.Text= rdr[0].ToString();//assuming sp_displaycostcenter returns costcenter.
        }
        rdr.Close();

    }        
    con.Close();
于 2013-05-09T05:25:15.213 回答
0

您可以使用 MSDN ( http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.aspx ) 中的标准代码块读取数据,如下所示,并用您自己的变量替换变量:

private static DataSet SelectRows(DataSet dataset,
                                  string connectionString,
                                  string queryString) 
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand( queryString, connection);
        adapter.Fill(dataset);
        return dataset;
    }
}
于 2013-05-09T05:28:02.153 回答