locked
Please help me to remove new line in constant error.Please help me out..its urgent RRS feed

  • Question

  • User2090702857 posted

    Here is my code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    
    
    
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void b1_click(object sender, EventArgs e)
        {
            UserManagement.AuthenticateUser(tb1.Text, tb2.Text, tb3.Text, tb4.Text, tb5.Text, tb6.Text, tb7.Text);
        }
        protected void cb1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
        protected void b2_Click(object sender, EventArgs e)
        {
            //Response.Redirect("Update page.aspx");
           // UseManagement.CreateUserAdapter( );
            
    
    
    
        }
    }
        
        namespace WindowsApplication1
    {
        public partial class register:Form
        {
            public register()
            {
                InitializeComponent();
             }
    //I am getting error here also..."InitializeComponent does not exist in this context"                                                                                                                                                              
    
    
    
    
    
              private void b2_Click(object sender, EventArgs e)
            {
                string connetionString = null;
                SqlConnection connection ;
                SqlDataAdapter adapter = new SqlDataAdapter();
                string sql = null;
                connetionString = "Data Source=SERVER NAME;Initial Catalog=DATABASE NAME;User ID=USER NAME;Password=PASSWORD";
                connection = new SqlConnection(connetionString);
               // sql="update tbluser2 SET Fullname='@Fullname',Address='@Address',Phonenumber='@Phonenumber',Username='@Username',Password='@Password',WHERE Emailid='@Emailid';//THIS IS THE LINE WHERE I AM GETTING ERROR "NEW LINE CONSTANT"
                try
                {
                    connection.Open();
                    adapter.UpdateCommand = connection.CreateCommand();
                    adapter.UpdateCommand.CommandText = sql;
                    adapter.UpdateCommand.ExecuteNonQuery();
                    MessageBox.Show ("Row updated !! ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
         }
      }

    Saturday, July 6, 2013 7:16 AM

Answers

  • User281315223 posted

    This was occurring because you were missing a closing double-quote at the end of your line (I also removed an extra comma after your password parameter : 

    sql="update tbluser2 SET Fullname='@Fullname',Address='@Address',Phonenumber='@Phonenumber',Username='@Username',Password='@Password' WHERE Emailid='@Emailid'";

    It should be noted that if you are using parameters I don't believe that you need the actual single quotes wrapping them (and you should also use a using statement for your actual connection itself) : 

    private void b2_Click(object sender, EventArgs e)
    {
          //Connection String
          string connetionString = "Data Source=SERVER NAME;Initial Catalog=DATABASE NAME;User ID=USER NAME;Password=PASSWORD";
    
          //Create your connection
          using (SqlConnection connection = new SqlConnection(connetionString))
          {
                //Your SQL query (you also had an extra comma after your @Password parameter
                string sql="UPDATE tbluser2 SET Fullname=@Fullname , Address=@Address, Phonenumber=@Phonenumber, Username=@Username, Password=@Password WHERE Emailid=@Emailid";
    
                //Open your connection
                connection.Open();
                
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.UpdateCommand = connection.CreateCommand();
                    adapter.UpdateCommand.CommandText = sql;
                    adapter.UpdateCommand.ExecuteNonQuery();
                    MessageBox.Show("Row updated !! ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
          }       
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 6, 2013 7:26 AM
  • User281315223 posted

    I don't see anywhere that an InitializeComponent method is declared (so that it would be usable).

    You would need to have a publically accessible method declared so that you could call it from within the register() method of your existing code as seen in the example below : 

    public register()
    {
           InitializeComponent();
    }
    
    public void InitializeComponent()
    {
           //Example
    }    

    The code I provided earlier should be enough to handle the using statement. Basically all that it does is properly close and dispose of your connections to avoid any future problems if a connection is called again. Your connection object will only exist within the actual scope of the using statement itself : 

    //Create your connection
    using (SqlConnection connection = new SqlConnection(connetionString))
    {
            //All of your Connection-related code here    
    }    
    

    It's just a much safer and preferred method for handling SqlConnections (or basically any other type of Connections or objects that inherit from IDisposible).


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 6, 2013 8:39 AM

All replies

  • User281315223 posted

    This was occurring because you were missing a closing double-quote at the end of your line (I also removed an extra comma after your password parameter : 

    sql="update tbluser2 SET Fullname='@Fullname',Address='@Address',Phonenumber='@Phonenumber',Username='@Username',Password='@Password' WHERE Emailid='@Emailid'";

    It should be noted that if you are using parameters I don't believe that you need the actual single quotes wrapping them (and you should also use a using statement for your actual connection itself) : 

    private void b2_Click(object sender, EventArgs e)
    {
          //Connection String
          string connetionString = "Data Source=SERVER NAME;Initial Catalog=DATABASE NAME;User ID=USER NAME;Password=PASSWORD";
    
          //Create your connection
          using (SqlConnection connection = new SqlConnection(connetionString))
          {
                //Your SQL query (you also had an extra comma after your @Password parameter
                string sql="UPDATE tbluser2 SET Fullname=@Fullname , Address=@Address, Phonenumber=@Phonenumber, Username=@Username, Password=@Password WHERE Emailid=@Emailid";
    
                //Open your connection
                connection.Open();
                
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    adapter.UpdateCommand = connection.CreateCommand();
                    adapter.UpdateCommand.CommandText = sql;
                    adapter.UpdateCommand.ExecuteNonQuery();
                    MessageBox.Show("Row updated !! ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
          }       
    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 6, 2013 7:26 AM
  • User2090702857 posted

    Thank you Rion Williams please tell me how to remove the error "InitializeComponent does not exist in this context" in the same code that I have posted..I have also this against the line where this error is occuring and also please tell me where and how to use using satatement plzzz

    Saturday, July 6, 2013 8:18 AM
  • User281315223 posted

    I don't see anywhere that an InitializeComponent method is declared (so that it would be usable).

    You would need to have a publically accessible method declared so that you could call it from within the register() method of your existing code as seen in the example below : 

    public register()
    {
           InitializeComponent();
    }
    
    public void InitializeComponent()
    {
           //Example
    }    

    The code I provided earlier should be enough to handle the using statement. Basically all that it does is properly close and dispose of your connections to avoid any future problems if a connection is called again. Your connection object will only exist within the actual scope of the using statement itself : 

    //Create your connection
    using (SqlConnection connection = new SqlConnection(connetionString))
    {
            //All of your Connection-related code here    
    }    
    

    It's just a much safer and preferred method for handling SqlConnections (or basically any other type of Connections or objects that inherit from IDisposible).


    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 6, 2013 8:39 AM
  • User2090702857 posted

    after writing the entire code as edited by you...then on feeding the details that should be updated in the database I am getting this error how to overcome it?

    I have attached the screen shot of tht exception message.Its in a pink coloured block

    
    

    Saturday, July 6, 2013 2:57 PM
  • User2090702857 posted

    I wanted to update data in database...for that I wrote code in C#...but on entering the data an exception is generated...here is my code,the error message....please reply everything that needs to be mentioned in the database on sql server 2008 in detail.I am sending stored procedure as well

    //code

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using System.Windows.Forms;
    
    
    
    
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void b1_click(object sender, EventArgs e)
        {
            UserManagement.AuthenticateUser(tb1.Text, tb2.Text, tb3.Text, tb4.Text, tb5.Text, tb6.Text, tb7.Text);
        }
        protected void cb1_SelectedIndexChanged(object sender, EventArgs e)
        {
    
        }
    
        
        
          protected void b2_Click(object sender, EventArgs e)
          {
              //SqlConnection connection;
    
     
                string connectionString = "Data Source=server name;Initial Catalog=database name;User ID=user id;Password=password";
                
    
               using (SqlConnection connection=new SqlConnection (connectionString))
              {
                
                 string sql="update tbluser2 SET Fullname=@Fullname Address=@Address,Phonenumber=@Phonenumber,Username=@Username,Password=@Password WHERE Emailid=@Emailid";
                 connection.Open();
                try
                {
                    SqlDataAdapter adapter = new SqlDataAdapter();
                    
                    adapter.UpdateCommand = connection.CreateCommand();
                    adapter.UpdateCommand.CommandText = sql;
                    adapter.UpdateCommand.ExecuteNonQuery();
                    MessageBox.Show ("Row updated !! ");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
           
      }
    }
    • //stored procedure

      USE [dbVocab]
      GO
      /****** Object: StoredProcedure [username] Script Date: 07/08/2013 00:58:52 ******/
      SET ANSI_NULLS ON
      GO
      SET QUOTED_IDENTIFIER ON
      GO
      ALTER PROCEDURE [username]

      @vFullname varchar(50),
      @vAddress varchar(50),
      @vPhonenumber varchar(50),
      @vEmailid varchar(50),
      @vUsername varchar(50),
      @vPassword varchar(50),
      @vConfirmPassword varchar(50)

      AS
      BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;


      END

      //The error that is generated

      Declare scalar variable @Fullname at System.Data.SqlClient.SqlConnection

      ---------------------''....---------------at System.Data.SqlClient.SqlInternalConnection

      -------------------''---------------------System.Data.SqlClient.Tds Parser.Throw Exception and Warning

      ............................''................................System.Data.SqlClient.Tds Parser.Try Run

      ......................''....................................System.Data.SqlClient.Sql Command.Run Execute Non-Query

      ----------''------------------------System.Data.SqlClient.Sql Command.Internal.Execute Non-Query

      ---------''.-------------------------System.Data.SqlClient.Sql Command.Execute Non-Query

      --

      -



    Saturday, July 6, 2013 3:31 PM