Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my code down stairs but it showing me and error saying unassigned local variable 'values'
C#
private void creategvreceptionist()
    {
        //int value = 0;
        //DataTable mdt;
        //DataRow mrow;
        string[] values;
        //string[] values1;
        DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time , tmslot_id from tblValidAppointmentTime ");

        foreach (DataRow row in dtreceptionisttmslot.Rows)
        {

            
            string q = "select v.first_appointment_time,v.tmslot_id,e.pc_eventDate,p.DOB,p.fname,p.lname,e.pc_eid FROM tblValidAppointmentTime As v,tblPatientData as p, tblOpenemrPostcalendarEvents as e where e.pc_eventDate>='"+DateTime.Now.ToShortDateString()+"'";
            DataTable dtreceptionist = (DataTable)cnt.GetRecord(q);

            if (dtreceptionist.Rows.Count > 0)
            {                
                foreach (DataRow arow in dtreceptionist.Rows)
                {
                   //DataRow brow = dtreceptionist.NewRow();
                   //arow["fname"] = dtreceptionist.Rows[0][1];
                   //brow["patient name"] = dtreceptionist.Rows[0][2];               
                    
                   values[0] = row["fname"].ToString();
                   values[1]= arow["patient name"].ToString();
                   values[2]= arow["DOB"].ToString();

                   dtreceptionisttmslot.Rows.Add(arow);
                }
            }
            else
            {
                //Values[0] = row["time"].ToString();
                //Values[1] = null;
                //Values[2] = null;
                //dtreceptionisttmslot.Rows.Add();
            }
        }
            dtreceptionisttmslot.AcceptChanges();
            gvreceptionist.DataSource = dtreceptionisttmslot;
            gvreceptionist.DataBind();
        }

does anybody have solution for this?

Thank you
Posted
Updated 21-Sep-12 4:29am
v3
Comments
[no name] 21-Sep-12 10:36am    
You mean this "string[] values;" that you have not assigned any kind of value to?
Member 9413472 21-Sep-12 10:41am    
yes, how am I suppose to be adding the value please advise.
[no name] 21-Sep-12 10:45am    
Well you would start by reading a basic programming book. For this particular example you need to "string[] values = new string[some-value-for-the-array-bounds];"
Member 9413472 21-Sep-12 10:50am    
thanks

if u want to bind the grid with two table then firstly u should merge both table in single table but for merging structure of both table should be same .then u can try this
// for merging tables
foreach (DataRow dr in table1.Rows)
{
table2.Rows.Add(dr.ItemArray);
}


After merging tables u can bind grid

gridview1.DataSource=table2;
grdview1.DataBind();

Hope this will help u.
Thanks
 
Share this answer
 
Comments
sammikumar 25-Jul-14 4:49am    
I got what i need.
You wrote:
C#
string[] values;

Post that, you directly use it:
C#
values[0] = row["fname"].ToString();
values[1]= arow["patient name"].ToString();
values[2]= arow["DOB"].ToString();

When did you initialize the variable? When did you wrote it is going to hold 3 value?

You need to define the variable before starting to use it and assign values.
 
Share this answer
 
Comments
Member 9413472 21-Sep-12 10:47am    
Hey Sandeep,
I have defined the variable initially
{
//int value = 0;
//DataTable mdt;
//DataRow mrow;
string[] values;
//string[] values1;
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time , tmslot_id from tblValidAppointmentTime ");
Member 9413472 21-Sep-12 10:49am    
Hey Sandeep sorry ignore earlier comment , please could you specify how i can initialize the variable to hold the three values. Thanks
Sandeep Mewara 21-Sep-12 10:56am    
string[] arr4 = new string[3];
arr4[0] = "one";
arr4[1] = "two";
arr4[2] = "three";
Sandeep Mewara 21-Sep-12 11:17am    
Correct this line:
values[0] = row["fname"].ToString();
to
values[0] = arow["fname"].ToString();
Member 9413472 21-Sep-12 11:21am    
see My code is like this

private void creategvreceptionist()
{
//int value = 0;
//DataTable mdt= new DataTable();

string[] values = new string[3];
//string[] values1;
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");

foreach (DataRow row in dtreceptionisttmslot.Rows)
{

//DataTable dtreceptionist = (DataTable)cnt.GetRecord("SELECT p.id,p.fname,p.lname,p.DOB,v.facility_name,v.provider_name,v.first_appointment_time,v.tmslot_id,v.timeslot_id,e.pc_pid, e.pc_eid, e.pc_aid, e.pc_title, e.pc_eventDate, e.pc_startTime, e.pc_hometext, u.fname, u.lname, u.mname FROM tblPatientData AS p,tblValidAppointmentTime AS v,tblOpenemrPostcalendarEvents AS e, tblUsers AS u WHERE e.pc_eventDate >= '" + DateTime.Now.ToShortDateString() + "' AND u.id = e.pc_aid AND p.id=e.pc_pid AND e.pc_tmslot_id=v.tmslot_id ");
string q = "select fname,DOB from tblPatientData";
DataTable dtreceptionist = (DataTable)cnt.GetRecord(q);

if (dtreceptionist.Rows.Count > 0)
{

foreach (DataRow arow in dtreceptionist.Rows)
{
//DataRow brow = dtreceptionist.NewRow();
//arow["fname"] = dtreceptionist.Rows[0][1];
//brow["patient name"] = dtreceptionist.Rows[0][2];



values[0] = row["first_appointment_time"].ToString();
//values[1] = row["tmslot_id"].ToString();
values[1]= arow["fname"].ToString();
values[2]= arow["DOB"].ToString();
//values[4] = arow["pc_pid"].ToString();


dtreceptionist.Rows.Add(values);
}
}
else
{
values[0] = row["time"].ToString();
values[1] = null;
values[2] = null;
//dtreceptionisttmslot.Rows.Add();
}
}
dtreceptionisttmslot.AcceptChanges();
gvreceptionist.DataSource = dtreceptionisttmslot;
gvreceptionist.DataBind();


}
still its getting me that error saying input array is longer than the columns in this table

Adding to Sandeep's answer, change the order in which you do things so that your initialization works better.
You need to get your data before you can initialize the array to its required size.
C#
string[] values;
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");

if( dtreceptionisttmslot != null && dtreceptionisttmslot.Rows.Count > 0 )
{
   values = new string[dtreceptionisttmslot.rows.count];
   // ... Now do your processing
}

My personal take is that this would work better
C#
List<string> values = new List<string>();
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");
foreach( DataRow dr in dtreceptionisttmslot.rows )
{
   // Do your processing and when you are ready to add a new item into the list do this
   values.Add( yourNewValue );
}
 
Share this answer
 
v2
Comments
Member 9413472 21-Sep-12 11:57am    
hello marcus
Thank for your answer i have use your above approach but still its getting me that error. Do you have any solution.
fjdiewornncalwe 21-Sep-12 12:01pm    
On what specific line is the error occuring? Use your debugger to find out and let us know so that we can focus in on the issue better.
Member 9413472 21-Sep-12 12:06pm    
hello

here is my code now

private void creategvreceptionist()
{

string[] values;
DataTable dtreceptionisttmslot = (DataTable)cnt.GetRecord("select first_appointment_time from tblValidAppointmentTime ");
if (dtreceptionisttmslot != null && dtreceptionisttmslot.Rows.Count > 0)
{
values =new string[dtreceptionisttmslot.Rows.Count];
foreach (DataRow row in dtreceptionisttmslot.Rows)
{

//DataTable dtreceptionist = (DataTable)cnt.GetRecord("SELECT p.id,p.fname,p.lname,p.DOB,v.facility_name,v.provider_name,v.first_appointment_time,v.tmslot_id,v.timeslot_id,e.pc_pid, e.pc_eid, e.pc_aid, e.pc_title, e.pc_eventDate, e.pc_startTime, e.pc_hometext, u.fname, u.lname, u.mname FROM tblPatientData AS p,tblValidAppointmentTime AS v,tblOpenemrPostcalendarEvents AS e, tblUsers AS u WHERE e.pc_eventDate >= '" + DateTime.Now.ToShortDateString() + "' AND u.id = e.pc_aid AND p.id=e.pc_pid AND e.pc_tmslot_id=v.tmslot_id ");
string q = "select fname,DOB from tblPatientData";
DataTable dtreceptionist = (DataTable)cnt.GetRecord(q);

if (dtreceptionist.Rows.Count > 0)
{

foreach (DataRow arow in dtreceptionist.Rows)
{




values[0] = row["first_appointment_time"].ToString();

values[1] = arow["fname"].ToString();
values[2] = arow["DOB"].ToString();


dtreceptionisttmslot.Rows.Add(values);
}
}
else
{
values[0] = row["time"].ToString();
values[1] = null;
values[2] = null;
//dtreceptionisttmslot.Rows.Add();
}
}
}
dtreceptionisttmslot.AcceptChanges();
gvreceptionist.DataSource = dtreceptionisttmslot;
gvreceptionist.DataBind();


}

when i am adding the value with the line dttimeslottmslot.row.add(values) that line shows me an error input array is longer than the columns in this table
fjdiewornncalwe 21-Sep-12 12:11pm    
That simply means that the you have to have the same number of items in your values[] array as columns in your dttimeslottmslotrow. Currently we can't see where you initialize dtreceptionisttmslot, but see how many columns are defined in that datatable and make sure that it is the same as the number of item sin your values[] array.
As a side note, your variable are incredibly difficult to read the way they are written. You should adopt camel-casing for them as that is the standard for C#. (ie. dtreceptionisttimslot should be dtReceptionistTimSlot)
Member 9413472 21-Sep-12 12:20pm    
Yes Thank you for your answer What I am doing over here is m using two datatable anf then getting one column from the first datatable and two columns from the second datatable depending on the first one.Is this the correct way I have done in my coding? If no then Can you please give me what should be the better solution for this?

Thank you
Your DataTable dtreceptionisttmslot contain only one field 'first_appointment_time' and u r going to add three fields inside the loop thats why the error is there
just improve your Query like below

"select first_appointment_time, ' ' as fname,' ' as DOB from tblValidAppointmentTime "

wish this will help

if not then reply, I will give u diffrent solution
 
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