Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
SqlDataAdapter adapter2 = new SqlDataAdapter("SELECT * FROM dbo.OrderLines", cn);

adapter2.InsertCommand = new SqlCommand("dbo.InsertOrderLine", cn);
adapter2.InsertCommand.CommandType = CommandType.StoredProcedure;

adapter2.InsertCommand.Parameters.Add(new SqlParameter("@ServiceProviderID", SqlDbType.Int, 15, "ServiceProviderID"));
adapter2.InsertCommand.Parameters.Add(new SqlParameter("@ProductID", SqlDbType.Int, 15, "ProductID"));
adapter2.InsertCommand.Parameters.Add(new SqlParameter("@PolicyNumber", SqlDbType.NVarChar, 50, "PolicyNumber"));

SqlParameter parameter2 = adapter.InsertCommand.Parameters.Add("@ID2", SqlDbType.Int, 0, "OrderLineID");
parameter2.Direction = ParameterDirection.Output;

DataTable orderline = new DataTable();
adapter.Fill(orderline);

// find primary key of both productComboBox.SelectedItem from Products table
// and serviceProviderCombobox.SelectedItem from ServiceProviders table
DataRow newRow2 = orderline.NewRow();
newRow2["ServiceProviderID"] = ?serviceProviderComboBox.SelectedItem?;
newRow2["ProductID"] = ?productComboBox.SelectedItem?;
newRow2["PolicyNumber"] = policyTextBox.Text;
orderline.Rows.Add(newRow2);

adapter.Update(orderline);

id2 = int.Parse(parameter2.Value.ToString());
Posted
Updated 20-Aug-14 2:41am
v2
Comments
[no name] 20-Aug-14 8:23am    
Is there a question mixed in with your unformatted code somewhere?
Thanks7872 20-Aug-14 8:41am    
:laugh: :laugh: :laugh: :laugh:
Member 11012401 20-Aug-14 8:30am    
I just added question marks around serviceProviderComboBox.SelectedItem and productComboBox.SelectedItem. I don't know what put here. Sorry, I am very new to this. I apologise if it is a mess.

Realised that the "SelectedValue" already contained the primary key. Assigned it to variables.

Added these two lines:

int spid = int.Parse(serviceProviderComboBox.SelectedValue.ToString());
int pid = int.Parse(productComboBox.SelectedValue.ToString());
 
Share this answer
 
you must write a select query that return primary key

query= select combobox.selectedvalue(or selected text) from table.(yourtable name)
where primarykey= X Y or else
 
Share this answer
 
Comments
Member 11012401 20-Aug-14 9:49am    
Thank you for the response. As i said, i'm still very new to c# so i am not sure exactly how to implement your solution. would it look something like this? ...

SqlCommand cmd = new SqlCommand("SELECT serviceProviderComboBox.SelectedValue FROM ServiceProviders WHERE ServiceProviderID = ServiceProviderID", cn);

cmd.CommandType = CommandType.Text;

SqlDataReader reader;

cn.Open();
reader = cmd.ExecuteReader();
cn.Close();

int spid = Convert.ToInt32(reader.ToString());

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