Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Posted
Updated 27-Aug-12 19:07pm
v2
Comments
Ganesan Senthilvel 28-Aug-12 1:07am    
Code format
Ganesan Senthilvel 28-Aug-12 1:07am    
What is the error message?
Umapathi K 28-Aug-12 1:13am    
incorrect syntax near "FETCHPERSONS_AMT"
Sarrrva 28-Aug-12 2:59am    
show your Store procedure code?

the problem is in this line..
cmd = new SqlCommand("FETCHPERSONS_AMT", gn.cn());

to solve it accurately i need to see your connection code above gn.cnopen().Usually i write it in this way..
C#
SqlCommand cmd = new SqlCommand(myQuery, con);
cmd.Connection = con;

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

If it isn't your problem,then see gn.cn() is ok or not..
 
Share this answer
 
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();
                    }
                }
 
Share this answer
 
v2
Combobox.SelectedText
should be used instead Combobox.SelectedItem. Also opportunity for improvement, I see:

C#
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());
}
 
Share this answer
 
v2
Comments
Umapathi K 28-Aug-12 1:13am    
Error like., incorrect syntax near "FETCHPERSONS_AMT"
Umapathi K 28-Aug-12 1:13am    
ALTER PROCEDURE [dbo].[FETCHPERSONS_AMT]

@roomtype nvarchar(250)

AS
BEGIN
SELECT noofpersons,rent from roomtypes WHERE roomtype=@roomtype
END

this is SP
Kuthuparakkal 28-Aug-12 1:17am    
did my reply/solutn help you ?
Umapathi K 28-Aug-12 1:24am    
no

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900