获取输入字符串的格式是不正确的为什么

获取输入字符串的格式是不正确的为什么

本文介绍了获取输入字符串的格式是不正确的为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户首先在相应的文本框中输入id时,我们必须检查数据库中是否存在真正存在的ID,如果可用,我们需要在相应的文本框中显示它们(如果不可用)我们需要将消息显示为不可用的记录。

因为我已经编写了如下代码,但是输入字符串的格式不正确错误请告诉我为什么我得到错误以及代码是什么?



dal中的代码:

here when user enters the id in the respective textbox first we have to check the id really existing in the database or not if available we need display them in respective textboxes if not available we need to display the message as record not available.
for that i have written the code as following but getting the input string is not in correct format error please let me know why i'am getting the error and what is the code for that?

code in dal:

public int FindRecord(int Id)
       {
           con.Open();
           SqlCommand cmd = new SqlCommand("search_tblEmployee", con);
           try
           {
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@Id", Id);
               return cmd.ExecuteNonQuery();

           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               con.Close();
           }
       }





代码in bll:





code in bll:

public int FindRecord(int Id)
       {
           clsdal objdal = new clsdal();
           try
           {
               return objdal.FindRecord(Id);
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }



演示文稿图层代码隐藏:


in presentation Layer codebehind:

protected void btnfind_Click(object sender, EventArgs e)
     {

         try
         {

             clsbll objbll = new clsbll();
             int Id = Convert.ToInt32(txtid.Text);
             string Name = txtname.Text;
             int DepartmentId = Convert.ToInt32(txtdepartmentid.Text);
             string Location = txtlocation.Text;
             int Salary = Convert.ToInt32(txtsalary.Text);
             objbll.FindRecord(Id);
             lbldisplay.Text = "Record Found";

         }
         catch (Exception ex)
         {
             throw ex;
         }
         finally
         {
             objbll = null;
         }
     }

推荐答案

con.Open();

由于您的代码都没有为其设置值。 (它可能是你的一个文本框,但这不太可能,因为系统通常处理它们)



所以你需要确认 - 我们调试器来看看抛出异常并查找 null 值的行,可能后面跟着。和一种财产名称的方法。然后,您可以回顾一下代码并找出原因 - 我们不能:我们无法访问您的屏幕或HDD! :笑:

Since none of your code sets a value into it. (It could be one of your text boxes, but that's less likely because the system normally handles them)

So you need to confirm that - us the debugger to look at the line that throws the exception and look for a null value, probably followed by "." and a method of property name. Then you can look back through your code and work out why - we can't: we don't have access to your screen, or HDD! :laugh:


int Id = Convert.ToInt32(txtid.Text);
int DepartmentId = Convert.ToInt32(txtdepartmentid.Text);
int Salary = Convert.ToInt32(txtsalary.Text);



这三个值应该是整数。

如果它们中的任何一个不是整数,你试着将这些值转换为整数,你会遇到这个错误。

尝试使用 int.TryParse []仅转换整数类型。


These three values should be integer.
If any of them is not an integer and you try and convert these values to integers, you will run into this error.
Try using int.TryParse[^] to convert only integer types.


这篇关于获取输入字符串的格式是不正确的为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 21:47