Object reference not set to an instance of an object.(windows applications) [Resolved]

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

in my coding it shows the error:Object reference not set to an instance of an object.

public partial class UpdateCustomerInvoice : Form
{
public UpdateCustomerInvoice()
{
InitializeComponent();
}
string str;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
DataSet ds1;
private void UpdateCustomerInvoice_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
SqlCommand cmd = new SqlCommand("select custname,custaddress from tbl_customer", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
foreach (DataRow row in ds.Tables[0].Rows)
{
cmb_customername.Items.Add(row.ItemArray[0].ToString());
}
con.Close();
}

private void cmb_customername_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("select custname,custaddress from tbl_customer", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds1 = new DataSet();
da.Fill(ds1);
DataRow trow;
ds.Tables[0].PrimaryKey = (new DataColumn[]{//error shows here
ds .Tables [0].Columns [0]});//error shows here
trow = ds.Tables[0].Rows.Find(cmb_customername.Text);
txt_customeraddress.Text = trow["custaddress"].ToString();
}
}
}

Best,
Sudheep.



Responses

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

Up
0
Down

Resolved
Hi,

you have not initializa ds in below code block

private void cmb_customername_SelectedIndexChanged(object sender, EventArgs e) 

{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
con.Open();
SqlCommand cmd = new SqlCommand("select custname,custaddress from tbl_customer", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
ds1 = new DataSet();
da.Fill(ds1);
DataRow trow;
ds.Tables[0].PrimaryKey = (new DataColumn[]{//error shows here
ds .Tables [0].Columns [0]});//error shows here
trow = ds.Tables[0].Rows.Find(cmb_customername.Text);
txt_customeraddress.Text = trow["custaddress"].ToString();
}


so better instead of ds use ds1 whereever used in above block.

Regards,
Sunil

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

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

Up
0
Down
Hi,

Just check null or not. Lets see the code:

if(ds != null)

{
ds.Tables[0].PrimaryKey = (new DataColumn[]{//error shows here


Thanks,
Arefin

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

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

Up
0
Down

Hi,
if (ds != null && ds.Tables.Count > 0)

{
// Write your code here
}


Check table count also as mentioned above.

Regards,
Sunil

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

Login to post response