Error:'drpEmpCode' has a SelectedValue which is invalid because it does not exist in the list of ite [Resolved]

Posted by Suniluc under C# on 2/7/2013 | Points: 10 | Views : 1001 | Status : [Member] | Replies : 10
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\vip\\BVIS\\BVIS\\App_Data\\VehicleDb.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
con.Open();
cmd.CommandText = "select * from tblalloc where VNo='" + drpVno.Text.Trim() + "'";
SqlDataReader rdr = cmd.ExecuteReader();
//rdr = cmd.ExecuteReader();
bool temp = false;
while (rdr.Read())
{
drpVno.Text = rdr.GetValue(0).ToString();
drpEmpCode.Text = rdr.GetString(2);
drpDrCode.Text = rdr.GetString(1);
drpBrCode.Text = rdr.GetString(3);
drpPrps.Text = rdr.GetString(4);
temp = true;
}
if (temp == false)
{

string tmp = "";
tmp = "<script language='javascript'>";
tmp += "alert('Vehicle No. not found');";
tmp += "</script>";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyScript", tmp);

}

what is the problem with the code?




Responses

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down

Resolved
i got the answer...this code corrected the problem.. :)

if (rdr.GetString(1) == "")
{
drpEmpCode.ClearSelection();
}
else
{
drpEmpCode.Text = rdr.GetString(1);
}

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
I have detected the problem...
i have inserted blank value to the table.
on radiolist selction change event i have the code

drpEmpCode.Items.Clear();

So when i try to retrieve null value is not in drop down....how to solve this?

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
Can anyone help me to solve this plz....

regards..

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

Posted by: Kusumas on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Suniluc,

I think in this table your not getting EmpCode bcoz it is Null value or Empty. Check it once which value your getting from Database.

Thanks

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
yes..that is the problem...how to solve this?

regards

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
plz...help...

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

Posted by: Kusumas on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
You can insert the value in EmpCode column...

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
if it is a commercial vehicle...i cannot assign it to any employ...
thats my problem..


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

Posted by: Kusumas on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Suniluc,

You can use like this also....

Select isnull(EMPCODE,' No Data in EMPCODE ') from tblalloc instead of EmpCode column.


If(rdr.GetString(1) == "No Data in EMPCODE")
{
drpEmpCode.ClearSelection();
}
else
{
drpEmpCode.Text = rdr.GetString(1);
}

Thanks

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

Posted by: Suniluc on: 2/7/2013 [Member] Starter | Points: 25

Up
0
Down
thanks...

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

Login to post response