我怎么......请帮助我 [英] How do I... Please help me

查看:56
本文介绍了我怎么......请帮助我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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);

        }
    }
}











错误显示输入字符串的格式不正确。



我尝试了什么:



我想先使用Combobox,然后填充第二个组合框。

桌面设计

类别

Category_ID int未选中

Category_name nvarchar(50)已选中

Short_Name nvarchar(50)已检查

未选中



单位

Unit_ID int未选中

Unit_name nvarchar(50)已检查

Category_ID int已检查

未选中



请帮帮我..






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..

推荐答案

查看错误消息:

Look at the error message:
Input string was not in a correct format

这意味着您正在尝试将基于字符串的值更改为其他值,可能是整数或DateTime。

我注意到你的代码似乎回复了stings,经常使用ToString将值与基于字符串的数字进行比较:

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:

if (ddl_Category.SelectedIndex.ToString() != "-1")

哪个是愚蠢的:

Which is silly:

if (ddl_Category.SelectedIndex >= 0)

更容易阅读,效率更高。

然后有这个:

is easier to read and more efficient.
And then there is this:

objMedi .Category_ID = Convert.ToInt32(ddl_Category.SelectedValue.ToString());

由于ValueMember是Category_ID - INTEGER - 为什么要乱用字符串?



我建议你抛弃表格构建代码,直接从数据库中读取数据使用DataAdapter进入DataTable并将其用作DataSource。这样,您从下拉列表中读取的数据就已经是数字了,您不必费力。

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.


Quote:

错误显示输入字符串的格式不正确。

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



如果仔细观察,它还会告诉您问题在哪里代码。

错误消息告诉您问题出在输入字符串的包含中,而不是在代码中,我们无法猜测输入字符串中的内容或问题出现的位置。

-----

您的代码没有按照您的预期行事,或者您不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是比较它应该做什么。

调试器中没有魔法,它不知道你的代码应该做什么,它没有找到b ugs,它只是通过向您展示正在发生的事情来帮助您。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。



调试器 - 维基百科,免费的百科全书 [ ^ ]


掌握调试Visual Studio 2010 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

在Visual Studio中调试C#代码 - YouTube [< a href =https://www.youtube.com/watch?v=u-HdLtqEOogtarget =_ blanktitle =New Window> ^ ]



这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。


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.


这篇关于我怎么......请帮助我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆