Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have multiple values in string variable like
string=values('Aloo','fry','54',1,5)('DalTadka','fyr','34',1,5)
i want to insert these data in a single table in a multiple fields
like
table veg_table
id name detail price MenuType HotelId
1 Aloo fry 54 1 5
2 DalTadka fry 34 1 5


how i insert these data into veg_table through stored procedure
Posted
Comments
Md. Khaled Hussain 14-Nov-13 1:38am    
You can do it in two ways.
1. Using TableType parameter in sp (see http://stackoverflow.com/questions/10408457/passing-datatable-from-c-sharp-to-sql-server-2008)
2. Or use XML as parametere and using xquery get data to insert.
♥…ЯҠ…♥ 14-Nov-13 2:22am    
Do you want to insert after splitting the string thru sp or asp.net? parameter order is not going to change right?
Arvind Labhi 14-Nov-13 2:34am    
yes, i want to split the data and then insert the data in a table.
this would work like after every comma split and insert into new filed
and after parentheses table row is split and the data insert in the new row and again row split by comma and enter into new field.

1 solution

Hi Arvind,

I dont know what you are trying to ask here you want to insert data using SP, but you have tagged with Asp.Net.
I took ASP.Net as first priority and tried this
First split the string and then pass it to either insert query or to sp as you wish.
C#
protected void Page_Load(object sender, EventArgs e)
        {
string temp1 = @"('Aloo','fry','54',1,5)('DalTadka','fyr','34',1,5)";
            bool val = splitRow(temp1);
}

 private bool splitRow(string fullRow)
        {
            string inputRowVal=string.Empty;
            foreach (var row in fullRow.Split(')'))
            {
                inputRowVal = row.Replace("(", "");
                SplitAndInsert(Convert.ToString(inputRowVal));
            }
            return true;
        }

private bool SplitAndInsert(string inputString)
        {
            bool isInserted = false;
            string temp=string.Empty;
            int count = 0;
            string[] param=new string[5];
            foreach (var str in inputString.Split(','))
            {
                if (str.Contains("'"))
                {
                    param[count] = str.Replace("'", "");
                }
                else
                {
                    param[count] = str;
                }
                count++;
            }

            if (param != null && param.Count() == 5)
            {
                using(SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["connSpionshopString"].ConnectionString))
                {
                    connection.Open();
                    string sql =  "INSERT INTO yourTableName VALUES(@param1,@param2,@param3,@param4,@param5)";
                    SqlCommand cmd = new SqlCommand(sql,connection);
                    cmd.Parameters.Add("@param1", SqlDbType.VarChar).Value = param[0];
                    cmd.Parameters.Add("@param2", SqlDbType.VarChar).Value = param[1];
                    cmd.Parameters.Add("@param3", SqlDbType.Int).Value = Convert.ToInt32(param[2]);
                    cmd.Parameters.Add("@param4", SqlDbType.Int).Value = Convert.ToInt32(param[3]);
                    cmd.Parameters.Add("@param5", SqlDbType.Int).Value = Convert.ToInt32(param[4]);
                    cmd.CommandType = CommandType.Text;
                    if (cmd.ExecuteNonQuery()==1)
                    {
                        isInserted = true;
                    }
                }
            }
            return isInserted;
        }


If you pass SplitAndInsert(@"'Aloo','fry','54',1,5") like this then it will split and inserted in the database.
Just take it as reference.
Hope this helps you.

Regards,
RK
 
Share this answer
 
v2
Comments
♥…ЯҠ…♥ 14-Nov-13 2:55am    
Updated my answer as per your comments.... now try this lemme know
♥…ЯҠ…♥ 14-Nov-13 5:26am    
Dont you think this solution can be Up-voted?

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