Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to access columns dynamically, It works fine when i select two columns, but when i try to elect the third it gives an eror, the C# syntax is as follows,

C#
string connStr = "Server=localhost;Database=db_pentest;Uid=root;Pwd=#901770026V;";
        bool found = false;
        MySqlConnection conn = new MySqlConnection(connStr);

        conn.Open();

        MySqlCommand cmd;

        cmd = conn.CreateCommand();
        
        cmd.CommandText = "dynamicCols";//call stored procedure
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new MySqlParameter("@col", "country,IPRStart,IPREnd"));
                
        MySqlDataReader readIp = cmd.ExecuteReader();


The error i get is,

SQL
Unknown column 'IPR' in 'field list'


The stored procedure goes something like this,

SQL
SET @sql = CONCAT('SELECT ', col, ' FROM ..


where is pass the column names i wan to select to that single col parameter. but it wont let me send more than two.. it cuts off the trailing charters of IPREnd..??
can you please help me on this..i'm doing it in this way because i need to select random columns whenever i want. Thank you very much :)
Posted
Updated 15-Aug-12 20:29pm
v2

The likeliest reason is that the col parameter in your stored procedure is too small in length. So try increasing the length of that parameter in your procedure definition.
 
Share this answer
 
Comments
dhl_hh 16-Aug-12 2:51am    
Yess that was it !! thank you very much :)
Check your @col data type. I think you have used less character in varchar(20).
But you input is hvaing more than 20 chracter.
'country,IPRStart,IPREnd' = 23 character
So your input is get shrink so it is taking first 20 chracter.
Try this varchar(max); or some big value like 100
 
Share this answer
 
Comments
dhl_hh 16-Aug-12 2:52am    
Yess.you are correct!! it worked.. much appreciated :)
Wendelius 16-Aug-12 2:55am    
You're welcome :)

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