There is no row at position 0.it shows this error.

Posted by Sudheep.grandhe under C# on 12/14/2011 | Points: 10 | Views : 6410 | Status : [Member] | Replies : 6
Hi,

in my combobox selectchangedevent it shows the following error."There is no row at position 0."

private void cmb_companyname_SelectedIndexChanged(object sender, EventArgs e)
{
int invoiceno=0;
if (cmb_companyname.SelectedValue.ToString() != "system.data.datarowview" && cmb_companyname.SelectedValue.ToString() != "" && cmb_companyname.SelectedValue.ToString() == string.Empty)
{
invoiceno = Convert.ToInt32(cmb_companyname.SelectedValue);
}
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
con.Open();
SqlCommand cmd=new SqlCommand ("select t2.compname,t2.compaddress,t2.invoiceno,t2.orderno,t2.ondate,t2.invoicedate,t2.amountstatus,t2.netamount,t2.netvat,t2.tamount,t1.particulars,t1.quantity,t1.perprice,t1.amount from tbl_report2 t2 left join tbl_report1 t1 on t1.invoiceno=t2.invoiceno where t2.invoiceno ="+invoiceno+ " ",con );
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
if (ds.Tables.Count > 0)
{
txt_customeraddress.Text = ds.Tables[0].Rows[0]["compaddress"].ToString();//error shows here.
txt_invoiceno.Text = ds.Tables[0].Rows[0]["invoiceno"].ToString();
txt_orderno.Text = ds.Tables[0].Rows[0]["orderno"].ToString();
txt_invoicedate.Text = ds.Tables[0].Rows[0]["invoicedate"].ToString();
txt_ondate.Text = ds.Tables[0].Rows[0]["ondate"].ToString();
cmb_particulars.Text = ds.Tables[0].Rows[0]["particulars"].ToString();
txt_quantity.Text = ds.Tables[0].Rows[0]["quantity"].ToString();
txt_perprice.Text = ds.Tables[0].Rows[0]["perprice"].ToString();
txt_amount.Text = ds.Tables[0].Rows[0]["amount"].ToString();
txt_netamount.Text = ds.Tables[0].Rows[0]["netamount"].ToString();
txt_vat.Text = ds.Tables[0].Rows[0]["netvat"].ToString();
txt_totalamount.Text = ds.Tables[0].Rows[0]["tamount"].ToString();
if (Convert .ToString (ds .Tables [0].Rows [0]["amountstatus"])!=null && Convert .ToString (ds .Tables [0].Rows [0]["amountstatus"])!="")
{
if (ds .Tables[0].Rows [0]["amountstatus"].ToString().ToUpper ()=="PAID")
{
rb_paid .Checked =true ;
rb_unpaid .Checked =false ;
}
else
{
rb_unpaid .Checked =true ;
rb_paid .Checked =false ;
}
}

}

Best,
Sudheep.



Responses

Posted by: Hmanjarawala on: 12/14/2011 [Member] Bronze | Points: 25

Up
0
Down
change this condition

if(ds.Tables.Count>0)

to

if(ds.Tables.Count>0 && ds.Tables[0].Rows.Count>0)


this will fix ur error

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

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

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

Up
0
Down
Hi,

Put one more check along with if (ds.Tables.Count > 0)

it should be like below

if (ds.Tables.Count > 0) 

{
if(ds.Tables[0].Rows.Count > 0 )
{
//Write all your binding code here
}
}


Regards,
Sunil

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

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

Up
0
Down
Hi,

that was solved.but,it wont display the data.when i select value from combobox

Best,
Sudheep.

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

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

Up
0
Down
Fine, that means query itself would returns any record based on invoice no.
Check manually at server also whether data present at server for particual selected invoice no.

Regards,
Sunil

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

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

Up
0
Down
Hi,

i want to display a text message before updating any value.it shows this error.

Error 17 Operator '==' cannot be applied to operands of type 'bool' and 'string'

private void btn_update_Click(object sender, EventArgs e)
{
if (txt_customeraddress.Text == txt_amount.Text == txt_invoicedate.Text == txt_invoiceno.Text == txt_netamount.Text == txt_ondate.Text == txt_orderno.Text == txt_perprice.Text == txt_quantity.Text == txt_totalamount.Text == txt_vat.Text == string.Empty)//error shows here
{
MessageBox.Show("please change the value before update");
}
else
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd = new SqlCommand("update tbl_report2 set compname=@compname,compaddress=@compaddress,orderno=@orderno,ondate=@ondate,invoicedate=@invoicedate,amountstatus=@amountstatus,netamount=@netamount,tamount=@tamount where invoiceno=@id", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd.Parameters.AddWithValue("@id", id);
if (txt_invoicedate.Text != null && txt_invoicedate.Text != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}

cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
SqlConnection con1 = new SqlConnection("data source=mars-104;initial catalog=Sudheeptest;integrated security=true");
SqlCommand cmd1 = new SqlCommand("update tbl_report1 set compname=@compname,orderno=@orderno,invoicedate=@invoicedate,ondate=@ondate where invoiceid=@id", con1);
cmd1.Parameters.AddWithValue("@compname", cmb_companyname.Text);
cmd1.Parameters.AddWithValue("@orderno", txt_orderno.Text);
cmd1.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
cmd1.Parameters.AddWithValue("@ondate", txt_ondate.Text);
cmd1.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd1.Parameters.AddWithValue("@id", txt_invoiceno.Text);
con1.Open();
cmd1.ExecuteNonQuery();
con1.Close();
txt_amount.Text = "";
txt_customeraddress.Text = "";
txt_invoicedate.Text = "";
txt_invoiceno.Text = "";
txt_netamount.Text = "";
txt_ondate.Text = "";
txt_orderno.Text = "";
txt_perprice.Text = "";
txt_quantity.Text = "";
txt_totalamount.Text = "";
txt_vat.Text = "";
txt_vatpercentage.Text = "";
MessageBox.Show("updated successfully");
}


}

Best,
Sudheep.

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

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

Up
0
Down
Hi,

Try below code

if (txt_customeraddress.Text == string.Empty &&
txt_amount.Text == string.Empty &&
txt_invoicedate.Text == string.Empty &&
txt_invoiceno.Text == string.Empty &&
txt_netamount.Text == string.Empty &&
txt_ondate.Text == string.Empty &&
txt_orderno.Text == string.Empty &&
txt_perprice.Text == string.Empty &&
txt_quantity.Text == string.Empty &&
txt_totalamount.Text == string.Empty &&
txt_vat.Text == string.Empty)
{
MessageBox.Show("please change the value before update");
}


Regards,
Sunil

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

Login to post response