使用组合框从数据库获取价值 [英] Get Value from database using combobox

查看:81
本文介绍了使用组合框从数据库获取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gn.cnopen();
  cmd = new SqlCommand("FETCHPERSONS_AMT", gn.cn());
            cmd.Parameters.AddWithValue("@roomtype", comboBox2.SelectedItem);
            string s = comboBox2.SelectedItem.ToString();
            
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                txtrnoofpersons.Text = dr["noofpersons"].ToString();
                textBox2.Text = dr["rent"].ToString();
            }
        }



在这里,我从组合框传递了值,并从数据库中检索了值.该值应如上所述填写在文本框中.我通过使用此代码得到错误



Here i passed value from combo box and retrieving value from database. the value should be filled in text box as mentioned above. i am getting error by using this code

推荐答案

问题出在这一行..
the problem is in this line..
cmd = new SqlCommand("FETCHPERSONS_AMT", gn.cn());


为了准确地解决它,我需要在gn.cnopen()上方查看您的连接代码.通常,我是用这种方式编写的..


to solve it accurately i need to see your connection code above gn.cnopen().Usually i write it in this way..

SqlCommand cmd = new SqlCommand(myQuery, con);
cmd.Connection = con;


其中myQuery是查询字符串,con是SqlConnection.
因此,如果FETCHPERSONS_AMT是您的查询字符串,则将其用作..


where myQuery is the query string and con is SqlConnection.
So if FETCHPERSONS_AMT is your query sting,then use it as..

cmd = new SqlCommand(FETCHPERSONS_AMT, gn.cn());


如果不是您的问题,则请查看gn.cn()是否正常..


If it isn''t your problem,then see gn.cn() is ok or not..


您好,尝试这样,

Hi try like this,

string SQLConnectionStr="Your own connection";

SqlConnection sqlConnection = new SqlConnection(SQLConnectionStr);

DataTable dtDataTablesList = new DataTable();

sqlConnection.Open();
SqlCommand sqlCmd = new SqlCommand("FETCHPERSONS_AMT", sqlConnection);
sqlCmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@roomtype", comboBox2.text);
sqlCmd.ExecuteNonQuery();
SqlDataAdapter adptr = new SqlDataAdapter(sqlCmd);
adptr.Fill(dtDataTablesList);
sqlConnection.Close();

if (dtDataTablesList.Rows.Count > 0)
                {
                    
                    foreach (DataRow dr in dtDataTablesList.Rows)
                    {
                	txtrnoofpersons.Text = dr["noofpersons"].ToString();
                	textBox2.Text = dr["rent"].ToString();
                    }
                }


Combobox.SelectedText
应该改为使用Combobox.SelectedItem.还有改善的机会,我看到了:

Combobox.SelectedText
should be used instead Combobox.SelectedItem. Also opportunity for improvement, I see:

try
{

    string connect = "yourconnString";
    SqlConnection scn = new SqlConnection(connect);
    string sp = "FETCHPERSONS_AMT";

    SqlCommand spcmd = new SqlCommand(sp, scn);
    spcmd.CommandType = CommandType.StoredProcedure;

    spcmd.Parameters.AddWithValue("@roomtype", comboBox2.SelectedText);

    SqlDataReader dr;
    //check param value
    //MessageBox.Show(spcmd.Parameters["@roomtype"].Value.ToString());

    scn.Open();

    dr = spcmd.ExecuteReader();


    while (dr.Read())
    {
        txtrnoofpersons.Text = dr["noofpersons"].ToString();
        textBox2.Text = dr["rent"].ToString();
    }

    scn.Close();
    dr.Close();
}
catch (SqlException x)
{
    MessageBox.Show(x.Message.ToString());
}


这篇关于使用组合框从数据库获取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆