Click here to Skip to main content
15,906,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to retrieve "customer_id" from Customer table and insert it into

SQL
fare_tariff(tariff_id, customer_id, total_price)


So I retrieve the customer_id from Customer table as below:

C#
using (SqlCommand command = new SqlCommand("SELECT customer_id FROM Customer WHERE UserName = '" + username + "' Password = '"+password +"' ", connection))
                {
                    string cust_id = customer_id.ToString();
                    SqlDataReader myReader = command.ExecuteReader();

                     if (myReader.Read())
                        {
                            cust_id = myReader["customer_id"].ToString();
                        }

                    int c_id = Convert.ToInt32(cust_id);

                   myReader.Close();
                   custID(c_id);
                }


and insert the customer_id into table fare_tariff like below:

C#
using (SqlCommand command = new SqlCommand("INSERT INTO flight_reservation(tariff_id, customer_id, total_price) VALUES(@val1,@val2,@val3)", connection))
                {

                    command.Parameters.Add("@val1", SqlDbType.Int).Value = tariff_id;
                    command.Parameters.Add("@val2", SqlDbType.Int).Value = customer_id;
                    command.Parameters.Add("@val3", SqlDbType.VarChar).Value = total_price.ToString();

                    command.ExecuteNonQuery();
                }




I declared customer_id as a variable for storing customer_id.

Problem is: tariff_id and total_price inserted successfully but the column customer_id is null yet.

Help needed.
Posted
Comments
MuhammadUSman1 27-Apr-13 1:29am    
You check when you are inserting customer_id in db it has some values. or it is null.

1 solution

* Debug the program, make sure the value is getting passed correctly.
* Use SQL Server Profiler to check what is going to the DB
 
Share this answer
 

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