Click here to Skip to main content
16,017,726 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

I am getting the error as "use of unassigned local variable 'k1'" whenever i am compiling my project. My Code is :

C#
public DataSet wsGenerateBill(string OrderID)
        {
            string total1 = string.Empty;
            float FinalTotal = 0;
            int k1, k2 = 0;
            try
            {
                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand("SELECT OrderDetails.FoodID, OrderDetails.Qty, OrderDetails.Price, OrderDetails.Qty * OrderDetails.Price AS Total From OrderDetails Where (OrderID = '" + OrderID + "')", con);
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(sdr);
                con.Close();

                int m = od1.UpdateTotalAmtOfFood(OrderID);

                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    for( int i =0; i < dt.Rows.Count; i++)
                    {
                        DataRow dr = dt.Rows[i];
                        string foodid = dr["FoodID"].ToString();
                        string quantity = dr["Qty"].ToString();
                        string price = dr["Price"].ToString();
                        string total = dr["Total"].ToString();

                        int quantity1 = int.Parse(quantity);
                        int price1 = int.Parse(price);
                        float billtotal = float.Parse(total);

                        FinalTotal = FinalTotal + billtotal;  
                    }
                    object outletid = orders.getOutletIDByOrderID(OrderID);
                    string outletid1 = (string)outletid;

                    object exist = bill.getBillIDByOrderID(OrderID);

                    if (exist == null)
                        k1 = bill.CreateBill(OrderID, outletid1, FinalTotal,'0','0');

                      if (k1 > 0)
                         k2 = bill.UpdateGrandTotalOfBill(OrderID);
                    

                    SqlCommand cmd1 = new SqlCommand("SELECT Bill.BillID, Bill.OutletID, Bill.OrderID, [Order].CustomerID, Customer.Name AS CustomerName, [Order].DestinationPlace, [Order].VehicleNo, [Order].SeatNo, [Order].PaymentMode, [Order].OrderDate, [Order].DeliveryTime, [Order].DeliveryDate, Outlet.Name AS OutletName, Outlet.City AS OutletCity, OrderDetails.FoodID, Food.Name AS FoodName, OrderDetails.Qty, OrderDetails.Price, OrderDetails.Total AS FoodTotal, Bill.OrderTotalAmt, Bill.TaxAmt, Bill.GrandTotal FROM Bill INNER JOIN [Order] ON Bill.OrderID = [Order].OrderID INNER JOIN Customer ON [Order].CustomerID = Customer.CustomerID INNER JOIN Outlet ON Bill.OutletID = Outlet.OutletID INNER JOIN OrderDetails ON [Order].OrderID = OrderDetails.OrderID INNER JOIN Food ON OrderDetails.FoodID = Food.FoodID WHERE (Bill.OrderID = '" + OrderID + "')", con);
                    con.Open();
                    SqlDataReader sdr1 = cmd1.ExecuteReader();
                    DataSet ds1 = new DataSet();
                    DataTable dt1 = new DataTable();
                    dt1.Load(sdr1);
                    con.Close();

                    if (dt1.Rows.Count == 0)
                    {
                       DataTable table1 = new DataTable("Bill");
                       table1.Columns.Add("Result");
                       table1.Rows.Add("Null");
                       ds1.Tables.Add(table1);
                       return ds1;
                    }
                    else
                    {
                       ds1.Tables.Add(dt1);
                       return ds1;
                    }
                }                   
            }
            catch (Exception)
            {
                return null;
            }
        }


Please tell me where i am going wrong. I am new in this field.
Thanks in advance...
Posted
Updated 16-Jul-12 19:24pm
v2
Comments
rp786 17-Jul-12 1:28am    
Thanks it worked.....

Hi
The variable k1 is not assigned any value. use the below line as follows...
C#
int k1 = 0;
int k2 = 0;


Please let me still you have issues

Regards
Sebastian
 
Share this answer
 
v3
From your code it is quite clear that you have not assigned any value to k1.
So change the 3rd line of your code like below.
int k1 = 0, k2 = 0;
 
Share this answer
 
Hello use this code as like


public DataSet wsGenerateBill(string OrderID)
{
string total1 = string.Empty;
float FinalTotal = 0;
int k1=0;
int k2 = 0;
try
{
DataSet ds = new DataSet();
SqlCommand cmd = new SqlCommand("SELECT OrderDetails.FoodID, OrderDetails.Qty, OrderDetails.Price, OrderDetails.Qty * OrderDetails.Price AS Total From OrderDetails Where (OrderID = '" + OrderID + "')", con);
con.Open();
SqlDataReader sdr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(sdr);
con.Close();

int m = od1.UpdateTotalAmtOfFood(OrderID);

if (dt.Rows.Count == 0)
{
return null;
}
else
{
for( int i =0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string foodid = dr["FoodID"].ToString();
string quantity = dr["Qty"].ToString();
string price = dr["Price"].ToString();
string total = dr["Total"].ToString();

int quantity1 = int.Parse(quantity);
int price1 = int.Parse(price);
float billtotal = float.Parse(total);

FinalTotal = FinalTotal + billtotal;
}
object outletid = orders.getOutletIDByOrderID(OrderID);
string outletid1 = (string)outletid;

object exist = bill.getBillIDByOrderID(OrderID);

if (exist == null)
k1 = bill.CreateBill(OrderID, outletid1, FinalTotal,'0','0');

if (k1 > 0)
k2 = bill.UpdateGrandTotalOfBill(OrderID);


SqlCommand cmd1 = new SqlCommand("SELECT Bill.BillID, Bill.OutletID, Bill.OrderID, [Order].CustomerID, Customer.Name AS CustomerName, [Order].DestinationPlace, [Order].VehicleNo, [Order].SeatNo, [Order].PaymentMode, [Order].OrderDate, [Order].DeliveryTime, [Order].DeliveryDate, Outlet.Name AS OutletName, Outlet.City AS OutletCity, OrderDetails.FoodID, Food.Name AS FoodName, OrderDetails.Qty, OrderDetails.Price, OrderDetails.Total AS FoodTotal, Bill.OrderTotalAmt, Bill.TaxAmt, Bill.GrandTotal FROM Bill INNER JOIN [Order] ON Bill.OrderID = [Order].OrderID INNER JOIN Customer ON [Order].CustomerID = Customer.CustomerID INNER JOIN Outlet ON Bill.OutletID = Outlet.OutletID INNER JOIN OrderDetails ON [Order].OrderID = OrderDetails.OrderID INNER JOIN Food ON OrderDetails.FoodID = Food.FoodID WHERE (Bill.OrderID = '" + OrderID + "')", con);
con.Open();
SqlDataReader sdr1 = cmd1.ExecuteReader();
DataSet ds1 = new DataSet();
DataTable dt1 = new DataTable();
dt1.Load(sdr1);
con.Close();

if (dt1.Rows.Count == 0)
{
DataTable table1 = new DataTable("Bill");
table1.Columns.Add("Result");
table1.Rows.Add("Null");
ds1.Tables.Add(table1);
return ds1;
}
else
{
ds1.Tables.Add(dt1);
return ds1;
}
}
}
catch (Exception)
{
return null;
}
}
 
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