Click here to Skip to main content
15,906,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please see the below code:
It shows an error like invalid column name column_name and values.
Please help
C#
public static int Dynamicarray(string connectionString, Dictionary<string, object> parameters)
    {
        using (SqlConnection con = Database.GetConnection())
        {

            string column_name;
            string value;
            foreach (KeyValuePair<string, object> kvp in parameters)
            {
                column_name =kvp.Key+",";
                value = kvp.Key + ";";
                //cmdExecute.Parameters.AddWithValue(kvp.Key, kvp.Value);
            }
            SqlCommand cmdExecute = new SqlCommand("insert into tbl1 (column_name) values(value)",con);
            //cmdExecute.CommandType = System.Data.CommandType.StoredProcedure;
            //con.Open();
            return cmdExecute.ExecuteNonQuery();
        }
    }
Posted
Updated 17-May-11 4:01am
v2

hi Had u checked the database table tbl1 having the same column name as u given column_name once check it and try...
and u have to pass it as
"+column_name+"  and value also as "+value+"

surely it will work now...
Seshu
 
Share this answer
 
Comments
rahul dev123 17-May-11 7:56am    
I also do this. If i declare "+coloumn_name+" then it shown errors like unassigned local variables.
hiseshu 17-May-11 8:02am    
Can u tell me wt exactly u r doing here.. r u trying to insert the value for particular columnn then put the sql cmd in foreach loop and try.. may be it works ...
make a break point on this particular line

SqlCommand cmdExecute = new SqlCommand("insert into tbl1 (column_name) values(value)",con);


inspect the actual SQL Statement that is getting fired from the .NET Code, by using watch window or anything like that...and if still not got the error copy the SQL From the watch window and paste it in the SQL Server query analyzer to find the actual error....
 
Share this answer
 
foreach (KeyValuePair<string, object> kvp in parameters)
            {
                column_name =kvp.Key+",";
                value = kvp.Key + ";";
                SqlCommand cmdExecute = new SqlCommand("insert into tbl1 (column_name) values(value)",con);
                return cmdExecute.ExecuteNonQuery();
            }
 
Share this answer
 
C#
public static int Dynamicarray(string connectionString, Dictionary<string,object> parameters)
    {
        using (SqlConnection con = Database.GetConnection())
        {
 
            string column_name;
            string value;
            foreach (KeyValuePair<string,object> kvp in parameters)
            {
                column_name =kvp.Key+",";
                value = kvp.Value.ToString()+ ",";
                //cmdExecute.Parameters.AddWithValue(kvp.Key, kvp.Value);
            }
            column_name = column_name.Remove(column_name.LastIndexOf(","));
            value = value.Remove(value .LastIndexOf(","));
            SqlCommand cmdExecute = new SqlCommand("insert into tbl1 ("+column_name+") values("+ value+ ")",con);
            //cmdExecute.CommandType = System.Data.CommandType.StoredProcedure;
            //con.Open();
            return cmdExecute.ExecuteNonQuery();
        }
    }.


haven't tested, please fix if you find any typo's
hope this helps
 
Share this answer
 
v3

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