Click here to Skip to main content
15,909,091 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Iam creating one application where medicine_name is autocomplete text, base on that selection i want to populate Expiry_Date but whenever i run the code its given me Index was outside the bounds of the array exception, retrieve code i written on dataGridView1_CellEndEdit event,im using ms access db i dont know where is im wrong here im pasting my code

table structure is :
Med_id AutoNumber
Medicine_Name Memo
Number_of_tab Memo
Drawer_No Memo
Dealer_Name Text
Availability Number
Expiry_Date Date/Time (format : shortDatetime)


datagridview structure
Quantity Medicine_Name(Its a autocomplete text base on this i want to show Expiry_Date),Medicine_Cost, Manufacturer_Name,Batch_No,Expiry_Date , Rupees

What I have tried:

C#
private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
        {

 string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;


            string medicinename = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["Medicine_name"].Value);
            DateTime Expiry_Date = Convert.ToDateTime (dataGridView1.Rows[e.RowIndex].Cells["Expiry_Date"].Value);

            con.Open();
            cmd = new OleDbCommand("select Expiry_Date from Medicine_Available_Detail where Medicine_Name='" + medicinename + "'", con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Expiry_Date = Convert.ToDateTime (dr.GetValue(5));// exeception coming on this line
            }
            con.Close();


        }
Posted
Updated 14-Oct-16 9:04am
Comments
[no name] 13-Oct-16 12:30pm    
Simple. You are getting the data for one column and trying to access the data in your data reader that doesn't exist (you only have 1 column of information not 5).
Atul Rokade 13-Oct-16 13:01pm    
i made changes in this line Expiry_Date = Convert.ToDateTime(dr["Expiry_Date"]); but still value is not retrieving in database :( , pls tell me on which event should i put a code so i can retrieve a value
[no name] 13-Oct-16 13:19pm    
The event you are using should work. Did you try steeping through your code and finding out what the values of the variables are? Start there.
Atul Rokade 13-Oct-16 13:53pm    
i tried everything but failed
[no name] 13-Oct-16 15:01pm    
Since none of know what that means, what do you expect us to tell you? We can't run or debug your code for you. It's up to you to learn how to run your code in the debugger and find the error.

1 solution

here is i got solution

C#
string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;


            string medicinename = Convert.ToString(dataGridView1.Rows[e.RowIndex].Cells["Medicine_name"].Value);
            DateTime Expiry_Date=new DateTime (); 

            con.Open();
            cmd = new OleDbCommand("select Expiry_Date as Expiry_Date from Medicine_Available_Detail where Medicine_Name=@Medicine_Name",con);
            cmd.Parameters.AddWithValue("@Medicine_Name",medicinename);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                Expiry_Date = Convert.ToDateTime(dr["Expiry_Date"]);
                
            }
            dataGridView1.Rows[e.RowIndex].Cells["Expiry_Date"].Value = Expiry_Date; 
            con.Close();
 
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