Click here to Skip to main content
15,885,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (GridViewRow row in GridView1.Rows)
        {
            if (row.RowType == DataControlRowType.DataRow)
            {
                Label lblOrderId = (Label)row.FindControl("lblOrderId");
                Label Product = (Label)row.FindControl("lblProduct");
                Label ProductID = (Label)row.FindControl("lblProduct_Id");

                Label price = (Label)row.FindControl("lblUPrice");
                DropDownList Quanty = (DropDownList)row.FindControl("myDropDownList");
                Label total = (Label)row.FindControl("lblTotal");
              
                DataSet ds = new DataSet();
                SqlConnection con = new SqlConnection(StrConnection);
                con.Open();
                SqlCommand cmd = new SqlCommand();

                cmd.Parameters.Add("@Order_Id", SqlDbType.Int).Value = "0";
                cmd.Parameters.Add("@Product_Id", SqlDbType.Int).Value = int.Parse("0" + ProductID.Text);
                cmd.Parameters.Add("@Quantity", SqlDbType.NVarChar).Value = Quanty.Text;
                cmd.Parameters.Add("@Total_Price", SqlDbType.NVarChar).Value = total.Text;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "InsertOrder";
                cmd.Connection = con;

                using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                {
                    da.Fill(ds);
                    con.Close();
                    int KeyId = int.Parse("0" + ds.Tables[0].Rows[0][0].ToString());// i want this id ie order_Id for use to insert in other table but geting error and table contain data from order_id- 111 in database//
                }
                
                con.Close();
            }
        }
    }


[SP FROM OP'S COMMENT]
SQL
Create proc InsertOrder 
@Order_Id int, 
@Product_Id int, 
@Quantity nvarchar(50), 
@Total_Price nvarchar(50) 
AS 
  SET NOCOUNT ON 
  SET XACT_ABORT ON 

  BEGIN TRAN 
  SELECT * FROM dbo.Order_Table WHERE Order_Id = @Order_Id 

  begin 

    INSERT INTO dbo.Order_Table 
    ( 
      Product_Id , 
      Quantity , 
      Total_Price 
    ) 
    SELECT 
      @Product_Id , 
      @Quantity , 
      @Total_Price 

    select top 1 Order_Id from dbo.Order_Table order by 1 desc 
   
    end 
  COMMIT 

[/SP FROM OP'S COMMENT]
Posted
Updated 8-Dec-14 2:03am
v4
Comments
/\jmot 8-Dec-14 7:27am    
error in which part??
Rajnish D mishra 8-Dec-14 7:33am    
where i write a comment means int KeyId
ZurdoDev 8-Dec-14 7:28am    
The error should be clear, there is no data.
Rajnish D mishra 8-Dec-14 7:41am    
@ryan in my table have data and above insertion is done but in may dataset not contain any row





Create proc InsertOrder
@Order_Id int,
@Product_Id int,
@Quantity nvarchar(50),
@Total_Price nvarchar(50)


AS
SET NOCOUNT ON
SET XACT_ABORT ON

BEGIN TRAN
SELECT * FROM dbo.Order_Table WHERE Order_Id = @Order_Id

begin

INSERT INTO dbo.Order_Table
(
Product_Id ,
Quantity ,
Total_Price
)
SELECT
@Product_Id ,
@Quantity ,
@Total_Price

select top 1 Order_Id from dbo.Order_Table order by 1 desc
end

COMMIT
ZurdoDev 8-Dec-14 7:51am    
Is this the full SP? If so, there's a bit wrong with it. If not, all you need to do is debug to see what is happening.

Your below code is returning the error.

nt KeyId = int.Parse("0" + ds.Tables[0].Rows[0][0].ToString());


Change your last Using block with below code.

C#
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds);
con.Close();
if(ds!=null && ds.Tabels[0].Rows.Count>0)
   {
    int KeyId = int.Parse("0" + ds.Tables[0].Rows[0][0].ToString());
   }
}


Basically your query is returning no rows.
 
Share this answer
 
Comments
Praveen Kumar Upadhyay 8-Dec-14 7:38am    
Please check your SP, why it is not returning rows. Also add proper code so that you should not get the run time Exception. Please check my solution.
Rajnish D mishra 8-Dec-14 7:48am    
Please tell me can my proc is wrong



Create proc InsertOrder
@Order_Id int,
@Product_Id int,
@Quantity nvarchar(50),
@Total_Price nvarchar(50)


AS
SET NOCOUNT ON
SET XACT_ABORT ON

BEGIN TRAN
SELECT * FROM dbo.Order_Table WHERE Order_Id = @Order_Id

begin

INSERT INTO dbo.Order_Table
(
Product_Id ,
Quantity ,
Total_Price
)
SELECT
@Product_Id ,
@Quantity ,
@Total_Price

select top 1 Order_Id from dbo.Order_Table order by 1 desc
end

COMMIT
Praveen Kumar Upadhyay 8-Dec-14 7:54am    
Is Order_Id optional in dbo.Order_Table table? because in your Insert query you are not using Order_Id column and that is case where your insert query might is getting fail(if Order_Id can not be null) so select will not execute after that and your procedure will not return a Row.
Rajnish D mishra 8-Dec-14 8:08am    
Order_Id is my Primery key can i remove my order_id from procdure
Praveen Kumar Upadhyay 8-Dec-14 8:13am    
Bro then this is the problem, you have to pass a unique value to Order_Id while inserting.

My solution is proper and I have cleared your query. Now please accept the solution if it is solving your problem.
It should be clear you! The SP InsertOrder does not return any data so you can not read any row as there is none!
Overview you SP.
Use SQL profiler to see what is the actual query to run.
 
Share this answer
 
Comments
/\jmot 8-Dec-14 7:39am    
You need to Explain More ,this is not a suitable answer.
You need to Add Like Solution 2.
Kornfeld Eliyahu Peter 8-Dec-14 7:43am    
I don't think so.
Solution two gives a workaround to hide the error message, but it is clear that OP expected to get some answer so the real solution is to check why answer didn't came...
Remember, that the fact that an application runs without error does not mean it does its assignment good or at all!
Praveen Kumar Upadhyay 8-Dec-14 7:49am    
I totally agree with you Kornfield, but he has not posted the SP when I replied.
ZurdoDev 8-Dec-14 7:52am    
The OP has posted some or perhaps all of the SP and is trying to return data.
/\jmot 8-Dec-14 8:03am    
you just said about the problem, didn't give any solution.that's why i said.
and +1 for that.
"InsertOrder" may not give result all the time, it may happen. then what should i do??

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