使用文本框控件在c#中更新数据库 [英] update database in c# using textbox control

查看:405
本文介绍了使用文本框控件在c#中更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新我的数据库

i我错了:必须声明标量变量@ROLL。





i want to update my database
i am getting this wrong :Must declare the scalar variable "@ROLL".


if (nage.Text == cage.Text)
   {
       using (
SqlConnection con =
    new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString))
       {
           con.Open();
           using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UPDATE RESULT_TABLE  SET  NAGE = @NAGE   where ROLL  = @ROLL", con))
           {
               cmd.Parameters.AddWithValue("@NAGE", cage.Text);

               // cmd.Parameters.AddWithValue("@chk", chk.Checked);

               // cmd.Parameters.AddWithValue("@ROLL", roll.Text);
               cmd.ExecuteNonQuery();
           }
       }


   }
   else
   {
       Response.Write("AGES DID NOT MATCH......");

   }

推荐答案

取消注释以下行:

Uncomment the following line :
// cmd.Parameters.AddWithValue("@ROLL", roll.Text);


您正在使用两个参数@ROLL和@NAGE,但它只添加一个参数@NAGE。因此,当它试图搜索@ROLL的值时,它会抛出异常。代码已存在于您的代码库中,只需取消注释即可。



尝试使用以下代码:

You are using two parameters @ROLL and @NAGE but it is adding only one parameter @NAGE. So when it is trying to search value of @ROLL it throws you exception. The code is already present in your codebase just uncomment it.

Try with below code:
if (nage.Text == cage.Text)
   {
       using (
SqlConnection con =
    new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ConnectionString))
       {
           con.Open();
           using (SqlCommand cmd = new System.Data.SqlClient.SqlCommand("UPDATE RESULT_TABLE  SET  NAGE = @NAGE   where ROLL  = @ROLL", con))
           {
               cmd.Parameters.AddWithValue("@NAGE", cage.Text);
               cmd.Parameters.AddWithValue("@ROLL", roll.Text);

               cmd.ExecuteNonQuery();
           }
       }
 

   }
   else
   {
       Response.Write("AGES DID NOT MATCH......");
 
   }


这篇关于使用文本框控件在c#中更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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