“ExecuteNonQuery:CommandText属性尚未初始化” [英] "ExecuteNonQuery: CommandText property has not been initialized"

查看:132
本文介绍了“ExecuteNonQuery:CommandText属性尚未初始化”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Text;

public partial class CreateChallan : System.Web.UI.Page
{
    string connStr = ConfigurationManager.ConnectionStrings["amitpandeyConnectionString2"].ConnectionString;
      protected void btnUpdate_Click(object sender, EventArgs e)
        {
            StringBuilder strSql = new StringBuilder(string.Empty);

            SqlConnection con = new SqlConnection(connStr);
            SqlCommand cmd = new SqlCommand();

            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)
                   GridView1.Rows[i].Cells[0].FindControl("chkSelect");
                if (chkUpdate != null)
                {
                    if (chkUpdate.Checked)
                    {

                        string strDocketNo = GridView1.Rows[i].Cells[1].Text;
                        string strBranchCode = ((TextBox)
                            GridView1.Rows[i].FindControl("txtBranchCode")).Text;

                        string strDate = ((TextBox)
                            GridView1.Rows[i].FindControl("txtDate")).Text;

                        string strPKTS = ((TextBox)
                            GridView1.Rows[i].FindControl("txtPKTS")).Text;

                        string strActWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtActwt")).Text;

                        string strChargeWt = ((TextBox)
                            GridView1.Rows[i].FindControl("txtChargeWt")).Text;

                        string strMode = ((TextBox)
                            GridView1.Rows[i].FindControl("Mode")).Text;

                        string strChallanNo = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanNo")).Text;

                        string strChallanDate = ((TextBox)
                            GridView1.Rows[i].FindControl("ChallanDate")).Text;

                        string strVehicleNo = ((TextBox)
                            GridView1.Rows[i].FindControl("VehicleNo")).Text;

                        string strDescription = ((TextBox)
                            GridView1.Rows[i].FindControl("Description")).Text;

                       


                        string strUpdate =
                            "Update CreateDocket set BranchCode = '" + strBranchCode + "', Date = '" + strDate + "', PKTS = '" + strPKTS + "',ActWt = '" + strActWt + "',ChargeWt = '" + strChargeWt + "',Mode = '" + strMode + "',ChallanNo = '" + strChallanNo + "',ChallanDate = '" + strChallanDate + "',VehicleNo = '" + strVehicleNo + "',Description = '" + strDescription + "' WHERE DEocketNo ='" + strDocketNo + "'";

                        strSql.Append(strUpdate);
                    }
                }
            }
            try
            {
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = strSql.ToString();
                cmd.Connection = con;
                con.Open();
                cmd.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                string errorMsg = "Error in Updation";
                errorMsg += ex.Message;
                throw new Exception(errorMsg);
            }
            finally
            {
                con.Close();
            }
            
        }
       
        protected void chkSelect_CheckedChanged
                            (object sender, EventArgs e)
        {
            CheckBox chkTest = (CheckBox)sender;
            GridViewRow grdRow = (GridViewRow)chkTest.NamingContainer;
            TextBox txtBranchCode = (TextBox)grdRow.FindControl
                                                ("txtBranchCode");
            TextBox txtDate = (TextBox)grdRow.FindControl
                                              ("txtDate");

            TextBox txtPKTS = (TextBox)grdRow.FindControl
                                              ("txtPKTS");

            TextBox txtActWT = (TextBox)grdRow.FindControl
                                              ("txtActWT");

            TextBox txtChargeWt = (TextBox)grdRow.FindControl
                                              ("txtChargeWt");

            TextBox txtMode = (TextBox)grdRow.FindControl
                                              ("txtMode");

            TextBox txtChallanNo = (TextBox)grdRow.FindControl
                                              ("txtChallanNo");

            TextBox txtChallanDate = (TextBox)grdRow.FindControl
                                              ("txtChallanDate");

            TextBox txtVehicleNo = (TextBox)grdRow.FindControl
                                              ("txtVehicleNo");

            TextBox txtDescription = (TextBox)grdRow.FindControl
                                              ("txtDescription");

            if (chkTest.Checked)
            {
              
                txtChallanNo.ReadOnly = false;
                txtChallanDate.ReadOnly = false;
                txtVehicleNo.ReadOnly = false;
                txtDescription.ReadOnly = false;
                txtChallanNo.ForeColor = System.Drawing.Color.Red;
                txtChallanDate.ForeColor = System.Drawing.Color.Red;
                txtVehicleNo.ForeColor = System.Drawing.Color.Red;
                txtDescription.ForeColor = System.Drawing.Color.Red;
            }
            else
            {
                txtChallanNo.ReadOnly = true;
                txtChallanDate.ReadOnly = true;
                txtVehicleNo.ReadOnly = true;
                txtDescription.ReadOnly = true;
                txtChallanNo.ForeColor = System.Drawing.Color.Green;
                txtChallanDate.ForeColor = System.Drawing.Color.Green;
                txtVehicleNo.ForeColor = System.Drawing.Color.Green;
                txtDescription.ForeColor = System.Drawing.Color.Green;  
                
            }
        }
    }



最初我收到错误,因为对象没有设置为它所在的实例line txtVehicleNo.ReadOnly = false;但当我删除该行时,它向我显示了填充错误。


Intially I was reciving error as object not set to the instance it was in the line txtVehicleNo.ReadOnly = false; but when I removed that line then It showed me the fillowing error.

推荐答案

可能存在一些情况,您可能没有 strSql的值.ToString()然后你可能会得到SQL命令的空sql语句。调试并检查代码无法按预期运行的原因。
there may be a situation where you may not have value for strSql.ToString() then you may end up with empty sql statement for SQL command. debug and check why your code not behave as expected.


这篇关于“ExecuteNonQuery:CommandText属性尚未初始化”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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