Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;

namespace PRESCRIPTION_WRITER
{
    public partial class Medicine_Master : Form
    {
        
        CLSMedicine objMedi = new CLSMedicine();
        public Medicine_Master()
        {
            InitializeComponent();
           BindCat();
           
        }
      
        private void BindCat()
        {
            DataTable dt = objMedi.CreateTempTable(objMedi.GetReport2());

            DataRow dr;
            dr = dt.NewRow();
            dr["Short_Name"] = "--Select--";
            dr["Category_ID"] = "-1";
            dt.Rows.InsertAt(dr, 0);
            ddl_Category.DataSource = dt;

            ddl_Category.SelectedIndex = 0;
            ddl_Category.DisplayMember = "Short_Name";
            ddl_Category.ValueMember = "Category_ID";               
        }

        private void BindUnit(int Category_ID)
        {
            DataTable dt = objMedi.CreateTempTable(objMedi.GetReport1());

            DataRow dr;
            dr = dt.NewRow();
            dr["Unit_name"] = "--Select--";
            dr["Unit_ID"] = "-1";
            dt.Rows.InsertAt(dr, 0);
            ddl_Category.DataSource = dt;

            ddl_Unit.SelectedIndex = -1;
            ddl_Unit.DisplayMember = "Unit_name";
            ddl_Unit.ValueMember = "Unit_ID";
        }
      

        private void ddl_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddl_Category.SelectedIndex.ToString() != "-1")
            {

        objMedi .Category_ID  = Convert.ToInt32(ddl_Category.SelectedValue.ToString());  
                BindUnit(objMedi.Category_ID);  
              
            }
        }
     
 
    }  
  
        }
                     
        
    
and Class files code is :::

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace PRESCRIPTION_WRITER
{
    class CLSMedicine
    {string Constr = System.Configuration.ConfigurationManager.AppSettings["constr"].ToString();
        private string _Medicine_name, _Generic_Name, _Strength;
        private int _ID, _Unit_ID, _Category_ID;
        public int  ID
        {
            get { return _ID; }
            set { _ID = value; }
        }

        public string Medicine_name
        {
            get { return _Medicine_name; }
            set { _Medicine_name = value; }
        }
        public string Generic_Name
        {
            get { return _Generic_Name; }
            set { _Generic_Name = value; }
        }
        public string Strength
        {
            get { return _Strength; }
            set { _Strength = value; }
        }
        public int Unit_ID
        {
            get { return _Unit_ID; }
            set { _Unit_ID = value; }
        }
        public int Category_ID
        {
            get { return _Category_ID; }
            set { _Category_ID = value; }
        }

       
      
        public DataTable CreateTempTable(SqlDataReader oledr)
        {
            DataTable myTable = new DataTable();
            DataRow TableRow;
            //  myTable.Columns.Add(new DataColumn("SrNo.", typeof(string)));
            for (int intColumn = 0; intColumn < oledr.FieldCount; intColumn++)
            {
                myTable.Columns.Add(new DataColumn(oledr.GetName(intColumn).ToString(), typeof(string)));
            }
            while (oledr.Read())
            {
                TableRow = myTable.NewRow();
                for (int intColumn = 0; intColumn < oledr.FieldCount; intColumn++)
                {
                    TableRow[oledr.GetName(intColumn).ToString()] = oledr[intColumn].ToString();
                }
                myTable.Rows.Add(TableRow);
            }
            return myTable;
        }
        public SqlDataReader GetReport()
        {
            SqlParameter[] pram = new SqlParameter[1];
            pram[0] = new SqlParameter("@type", 1);
            return SqlHelper.ExecuteReader(Constr, "Medi", CommandType.StoredProcedure, pram);

        }
        public SqlDataReader GetReport1()
        {
            SqlParameter[] pram = new SqlParameter[2];
            pram[0] = new SqlParameter("@type", 5);
            pram[1] = new SqlParameter("@Category_ID", Category_ID);
            return SqlHelper.ExecuteReader(Constr, "Medi", CommandType.StoredProcedure, pram);

        }
        public SqlDataReader GetReport2()
        {
            SqlParameter[] pram = new SqlParameter[1];
            pram[0] = new SqlParameter("@type", 5);
            return SqlHelper.ExecuteReader(Constr, "Uni", CommandType.StoredProcedure, pram);

        }
        public int MedicineInsert()
        {

            try
            {
                SqlParameter[] pram = new SqlParameter[6];
                pram[0] = new SqlParameter("@Medicine_name", Medicine_name);
                pram[1] = new SqlParameter("@Generic_Name", Generic_Name );
                pram[2] = new SqlParameter("@Strength", Strength );
                pram[3] = new SqlParameter("@Unit_ID", Unit_ID);
                pram[4] = new SqlParameter("@Category_ID", Category_ID);
                pram[5] = new SqlParameter("@Type", 2);
                return SqlHelper.ExecuteNonQuery(Constr, "Medi", CommandType.StoredProcedure, pram);
            }
            catch
            {
                return -1;
            }
            finally
            {


            }
        }
        public int MedicineDelete()
        {

            SqlParameter[] pram = new SqlParameter[2];

            pram[0] = new SqlParameter("@Type", 4);
            pram[1] = new SqlParameter("@ID", ID);
            return SqlHelper.ExecuteNonQuery(Constr, "Medi", CommandType.StoredProcedure, pram);

        }
        public int MedicineUpdate()
        {
            SqlParameter[] pram = new SqlParameter[7];
            pram[0] = new SqlParameter("@Medicine_name", Medicine_name);
            pram[1] = new SqlParameter("@Generic_Name", Generic_Name);
            pram[2] = new SqlParameter("@Strength", Strength);
            pram[3] = new SqlParameter("@Unit_ID", Unit_ID);
            pram[4] = new SqlParameter("@Category_ID", Category_ID);
            pram[5] = new SqlParameter("@Type", 3);
            pram[6] = new SqlParameter("@ID", ID);
            return SqlHelper.ExecuteNonQuery(Constr, "Medi", CommandType.StoredProcedure, pram);

        }
    }
}





Error Show that"Input string was not in a correct format."

What I have tried:

I want to use first Combobox and after that populate Second Combo Box.
Table design
"Category"
Category_ID int Unchecked
Category_name nvarchar(50) Checked
Short_Name nvarchar(50) Checked
Unchecked
And
"Unit"
Unit_ID int Unchecked
Unit_name nvarchar(50) Checked
Category_ID int Checked
Unchecked

Please Help Me..
Posted
Updated 26-Nov-18 22:33pm
v2
Comments
lmoelleb 27-Nov-18 3:37am    
Run a debugger so you can see the exact line it fails on, then google the error message if it is not clear from the location and variable values (yes, even us professionals do that on the more obscure errors). If it does not solve it, make a smaller peace of code that reproduce the problem, then post it in a question using proper formatting so we can read it.
In general debugging is the primary skill to learn - you will spend more time in the debugger than typing code... so make learning it a priority.

Look at the error message:
Input string was not in a correct format
That means that you are trying to change a string based value to something else, probably a integer or a DateTime.
I notice that your code seems to reply of stings, frequently using ToString to compare values with string based numbers:
C#
if (ddl_Category.SelectedIndex.ToString() != "-1")
Which is silly:
C#
if (ddl_Category.SelectedIndex >= 0)
is easier to read and more efficient.
And then there is this:
C#
objMedi .Category_ID = Convert.ToInt32(ddl_Category.SelectedValue.ToString());
Since the ValueMember is Category_ID - an INTEGER - why mess about with strings at all?

What I'd suggest is that you throw the table building code away, and read your data from your DB directly into a DataTable using a DataAdapter and use that as the DataSource. That way, the data you read from your drop down can be numeric already and you don't have to faff with it.
 
Share this answer
 
Quote:
Error Show that"Input string was not in a correct format."

If you look carefully, it also tells you where the problem is in your code.
The error message tells you that the problem is in the contains of input string, and not in your code, we can' t guess what is in the input string or where the problem shows up.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 

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