Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
mycnOptionPanel.Open();
  mycmdOptionPanel = mycnOptionPanel.CreateCommand();             

  mycmdOptionPanel.CommandText = "select Device_ID from device_info where      Device_name=@Device_name";
                    mycmdOptionPanel.Parameters.Add("@Device_name",LabelDeviceName.Text);
                  
 string strDeviceID = mycmdOptionPanel.ExecuteScalar().ToString();


I want to select single value from table in database of mysql into strDeviceID variable, on last line strDeviceID variable contain null,why this is happend? is my code is wrong?
plz help.
Posted
Updated 15-Aug-15 23:13pm
v3
Comments
Kornfeld Eliyahu Peter 16-Aug-15 5:14am    
You may have no such device with @Device_name
Check directly from SQL Studio...
ven.in 16-Aug-15 5:21am    
i have.
Kornfeld Eliyahu Peter 16-Aug-15 5:22am    
As Device_name probably a string value you should enclose it in quotes - it may create a syntax problem otherwise and return the wrong result...
"select Device_ID from device_info where Device_name = '@Device_name'"
ven.in 16-Aug-15 5:35am    
ya i did but not working..plz see this exception
Object reference not set to an instance of an object.
ven.in 16-Aug-15 5:26am    
Object reference not set to an instance of an object. this exception is fired.

One problem is that the SqlParameterCollection.Add Method (String, Object)[^] is obsolete. Because of this, use AddWithValue[^] instead. In other words
C#
...
mycmdOptionPanel.Parameters.AddWithValue("@Device_name",LabelDeviceName.Text);
...

Also note that the parameter value must be an exact match with the value in the database. If you have case-sensitive database then even small and capital letters affect the result. If the ExecuteScalar returns no rows, the return value is null thus resulting into an exception.
 
Share this answer
 
v2
Comments
ven.in 16-Aug-15 5:54am    
i did same.but not working. thanx for reply
Wendelius 16-Aug-15 5:59am    
Try removing the WHERE condition. That should return you one row from the database. The row is most likely not the on you want but that would tell if the statement works or not.

I another words, use:

mycmdOptionPanel.CommandText = "select Device_ID from device_info";
string strDeviceID = mycmdOptionPanel.ExecuteScalar().ToString();
ven.in 16-Aug-15 7:35am    
yes tried this one it return 1st field in device_id column. hence query is executing properly but with where clause it is not working properly. may u help plz
Wendelius 16-Aug-15 7:47am    
If that is the case, then the problem seems to be with the condition.

Using debugger, check what is the value of "LabelDeviceName.Text". Copy this exact same value to the SQL Editor and execute the statement with any parameters. Do you get a row?

For example, if the value for LabelDeviceName.Text is XYZ, execute a statement like

select Device_ID from device_info where Device_name='XYZ'
ven.in 16-Aug-15 8:22am    
yes i got the row by select Device_ID from device_info where Device_name='XYZ'

why it is not working in the code. it is working in sql editor

is this case that @Device_name should same as name of in table i tried that also but no result.

mycmdOptionPanel.Parameters.Add("@Device_name",LabelDeviceName.Text);
mycmdOptionPanel.CommandText = "select Device_ID from device_info where device_info.Device_name='"+ LabelDeviceName.Text + "'";
// mycmdOptionPanel.Parameters.AddWithValue("@device",LabelDeviceName.Text);

sting strDeviceID=mycmdOptionPanel.ExecuteScalar().ToString() ;

i just commented bold lines and specifies value directly witout addwithparameter and its work.
 
Share this answer
 
Comments
Maciej Los 16-Aug-15 17:19pm    
Your code is Sql Injection vulnerable.
ven.in 17-Aug-15 0:38am    
yes i know but what else solution for this. thanx for reply
Maciej Los 17-Aug-15 1:58am    
First of all, you have to debug your program to find out where the problem is. Second of all, you did not provide any information abot mycmdOptionPanel! The code provided by you did not contain initialization for this component (whatever it is).
ven.in 17-Aug-15 2:54am    
void Load_Options_Panel()
{
MySqlConnection mycnOptionPanel = new MySqlConnection("Server=....");
MySqlCommand mycmdOptionPanel;

-
-
-
}

here i declared the mycnOptionPanel and mycmdOptionPane in function Load_Options_Panel()

i debug it but not found any problem rather it is not taking the value by using addwithparametervalue.

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