shows the error as There is no row at position 0. [Resolved]

Posted by Sudheep.grandhe under C# on 12/12/2011 | Points: 10 | Views : 33001 | Status : [Member] | Replies : 10
HI,

in my coding it shows the error.There is no row at position 0.here is my code.


private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("select custaddress from tbl_customer where custname=@custname", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@custname", cmb_companyname.SelectedValue .ToString ());
DataTable dt = new DataTable();
da.Fill(dt);
txt_customeraddress.Text = dt.Rows[0][0].ToString();
}

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Check it out in your database also whether same address present over there.
dt.Rows[0][0] means it will return first row and first column from datatable even though there r multiple records in datatable.
So be sure where companame is unique in database because i dont think it should be unique , so keep other parameter too along with compnay name to get proper address in case there r more than one company with same name.

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Put a check before accessing to any datatable or dataset .

txt_customeraddress.Text = dt.Rows[0][0].ToString(); 


replace with

if(dt.Rows.Count  > 0 )

{
txt_customeraddress.Text = dt.Rows[0][0].ToString();
}


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Previously i tried this one.But,it wont shows the customeraddress based on combobox selectedvalue.i was debug the code.it wont execute the inside of the if block.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
That means there is no record for particualar custname.
Best way to check keep break point and see what value would u get for selectedvalue of combobox.

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
the output of the query not returning any records.....that's y exception is raised .....

G.Varun

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

as you said i put the breakpoint and check the custname value it displays as 4

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

as you said i will check that query in sqlserver it will return the customeraddress.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e) 

{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("select custaddress from tbl_customer where custname=@custname", con);
cmd.Parameters.AddWithValue("@custname", cmb_companyname.Text.ToString ());
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
txt_customeraddress.Text = dt.Rows[0][0].ToString();
}


try above code ,
you did a mistake in ur previous code as u have assigned cmd object to adapter before assiging its value.
and one more thing jsut check it out whether u r assing companme or it id. If assinging name they you should not use selected value because value will returns it id binded to combobox.


Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Put count check also for datatable as mentioned in one post above.

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sudheep.grandhe on: 12/12/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

it is displaying same customeraddress for all companynames.

Best,
Sudheep.

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response