Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
The problem occurs when I run the page even though I am not getting any errors on .cs.
'Hex Mec' is the first CompanyName from Supplier table with a datatype varchar (even though the error says 'converting the nvarchar value'. I'm trying to Populate One Asp.net Dropdown based on Selection in Another Dropdown taken from this reference link

I have a lot of stack trace where the source error is instructing me to trace.
I'm also doing codes using datasettableadapters and would welcome anyone's suggestion (pleading, for this is a thesis I'm working on).

What I have tried:

I'm not a very good coder and would like to seek help in the simplest way. I have tried using parse but nothing seems to be working (the bold characters are where I think it's not working). I'm trying to Populate One Asp.net Dropdown based on Selection in Another Dropdown and my code is:

protected void ddSuppliers_SelectedIndexChanged(object sender, EventArgs e)
    {
        int SupplierID = Convert.ToInt32(ddSuppliers.SelectedValue);
        SqlConnection con = new SqlConnection(connString);
        con.Open();
        SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[RawMaterialMF] WHERE SupplierID=@SupplierID", con);
        cmd.Parameters.Add("@SupplierID", SqlDbType.Int).Value = SupplierID;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
ddRawMats.DataTextField = "ItemName";
        ddRawMats.DataValueField = "ItemName"; 
        ddRawMats.DataBind();
        ddRawMats.Items.Insert(0, new ListItem("Select Raw Materials", "0"));


where this dropdown also appears in:
protected void btnProceed_Click(object sender, EventArgs e)
    {
        ddSuppliers.Enabled = false;
        mvPDetails.ActiveViewIndex = 0;

        //getting ItemID using the ItemName from the dropdown list
        DataSetTableAdapters.PurchaseDetailsTableAdapter datItem = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        DataTable tblPurchase = new DataTable();
        tblPurchase = datItem.GetDataByItem(ddRawMats.Text);
        int Item = Convert.ToInt32(tblPurchase.Rows[0]["ItemID"].ToString());

        //getting SupplierID
        DataSetTableAdapters.PurchaseDetailsTableAdapter datSuppliers = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        DataTable tblSuppliers = new DataTable();
        tblSuppliers = datSuppliers.GetDataBySupplier(ddSuppliers.SelectedValue);
        int Supplier = Convert.ToInt32(tblSuppliers.Rows[0]["SupplierID"].ToString());

        //getting Price using the ItemName from the dropdown
        DataSetTableAdapters.RawMaterialMF1TableAdapter datPrice = new DataSetTableAdapters.RawMaterialMF1TableAdapter();
        DataTable tblPrice = new DataTable();
        tblPrice = datPrice.GetDataByPrice(Item);
        int Price = Convert.ToInt32(tblPrice.Rows[0]["Price"]);
        int total = Price * Convert.ToInt32(txtQty.Text.ToString());

        double qty;
        qty = Convert.ToDouble(txtQty.Text);

        //
        DataSetTableAdapters.PurchaseDetailsTableAdapter datInsert = new DataSetTableAdapters.PurchaseDetailsTableAdapter();
        datInsert.InsertPDetails(null, Item, qty, total, txtRemarks.Text, Supplier);


        gvPurchase.DataBind();
    }
Posted
Updated 26-Oct-17 22:58pm
v2

Exactly what value do you expect to get from trying to convert "Hex Mec" to an integer? It is obviously impossible for the computer to make any guesses as to what you want.
 
Share this answer
 
Comments
C I'm not Sharp 27-Oct-17 4:39am    
It's ID, an int. It's from a dropdown with a datasource that shows the list of companynames in the Supplier table, but I don't know why and what code is getting "Hex Mec" only when it should be getting all the companynames.
Richard MacCutchan 27-Oct-17 4:48am    
You cannot convert a string of alphabetic characters to an integer. Also in the line:
int Supplier = Convert.ToInt32(tblSuppliers.Rows[0]["SupplierID"].ToString()); and assuming that the SupplierId item is an integer, why are you converting it to a string just so you can convert it back to an integer?
If your column is supposed to be an integer ID field, why are you converting it to a string, in order to convert it back to an integer?
And if it isn't an integer - and it isn't if the data in your table is "hex mec" - then it probably should be, so problems like this don't occur.

We can't say "do this" and fix the problem - it's too tightly bound to your data and data source, neither of which we have any access to.
So, its going to be up to you.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Comments
C I'm not Sharp 27-Oct-17 4:44am    
I tried putting a breakpoint but it's not working. Even though I remove the ToString, the error's still the same. Do you think the problem is in the code, or my sql?
OriginalGriff 27-Oct-17 4:57am    
It's complaining about NVARCHAR - which means it's something to do with the database.
Exactly what is the problem - and unless you find exactly what code is complaining (and TBH I don't think it's the code you show) you aren't going to find out what.
If you run your app in the debugger, then the exception should be caught by the debugger unless your code is trapping it itself via a try...catch block. You can override a try...catch in the debugger via the "Debug...Exceptions" menu and ticking everything in the "Thrown" column.
When the debugger catches an exception, it halts at the line and you can look at exactly what is causing the problem (or at least what you asked SQL yo do that might indicate where in your DB the data is failing.)
C I'm not Sharp 27-Oct-17 5:22am    
I am not using try and catch in the whole .cs page which leaves me to the something in the database. Anyway thanks, I'm gonna try to do more experimenting on this
We cannot see how your are populating your DropDownList.
Generally, DropDownLists bound to a table need two informations: the key representing the selected value (a key because it must allow to retrieve a specific row in the table unambiguously), and the text used to display the selected value. These informations are controlled with DataValueField and DataTextField properties, respectively. So that when you select "Hex Mec" in the list, the value returned by the SelectedValue property is the primary key of the element in the database table.
You can see more on DropDownList usage on its reference page:
DropDownList Class[^]
Kindly.
 
Share this answer
 
Comments
C I'm not Sharp 27-Oct-17 5:25am    
This is what's in my aspx page.
<asp:DropDownList ID="ddSuppliers" runat="server" Width="100%" CssClass="form-control" DataSourceID="SqlDataSourceSuppliers" DataTextField="CompanyName" DataValueField="CompanyName" AutoPostBack="True" OnSelectedIndexChanged="ddSuppliers_SelectedIndexChanged">


<asp:SqlDataSource runat="server" ID="SqlDataSourceSuppliers" ConnectionString='<%$ ConnectionStrings:CECnewConnectionString %>' SelectCommand="SELECT [CompanyName], [SupplierID] FROM Supplier">

I haven't had the chance to select anything bec first of all, it's directing me to an error page. T
phil.o 27-Oct-17 6:04am    
Probably:
<asp:DropDownList ID="ddSuppliers" runat="server" Width="100%" CssClass="form-control" DataSourceID="SqlDataSourceSuppliers" DataTextField="CompanyName" DataValueField="SupplierID" AutoPostBack="True" OnSelectedIndexChanged="ddSuppliers_SelectedIndexChanged">

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