locked
How to Inserting true to row while inserting other records RRS feed

  • Question

  • User-2074858223 posted

    I want to how to Insert true into CommunityStatus while inserting record

     protected void btnInvite_Click(object sender, EventArgs e)
        {
            if (Session["UserName"] != null && Session["UserName"].ToString() != string.Empty)
            {
    
                string username = Session["UserName"].ToString();
                GetUserScraps(username);
            }
            else
            {
    
            }
        }
    
    
        public void GetUserScraps(string username)
        {
    
    
            string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            string getADPOST = "Update CommunityFollow (MyUserName,FriendUserName,CommunityStatus=True) VALUES('" + Session["UserName"].ToString() + "','" + Request.QueryString["Id"].ToString() + "')";
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable ds = new DataTable();
    
                    da.Fill(ds);
                    
                    }
                }
            }

    ERROR MESSAGE

    Server Error in '/' Application.
    
    Incorrect syntax near '='.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.
    
    Source Error: 
    
    
    Line 58:                 DataTable ds = new DataTable();
    Line 59: 
    Line 60:                 da.Fill(ds);
    Line 61:                  if (ds.Rows.Count > 0)
    Line 62:                 {
    

    TABLE FOR UPDATE

    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    CREATE TABLE [dbo].[CommunityFollow](
    	[Id] [int] IDENTITY(1,1) NOT NULL,
    	[MyUserName] [nvarchar](500) NULL,
    	[FriendUserName] [nvarchar](500) NULL,
    	[CommunityStatus] [bit] NULL,
    	[Status] [nvarchar](50) NULL,
    	[SendDate] [datetime] NULL,
     CONSTRAINT [PK_CommunityFollow] PRIMARY KEY CLUSTERED 
    (
    	[Id] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    
    GO
    
    ALTER TABLE [dbo].[CommunityFollow] ADD  CONSTRAINT [DF_CommunityFollow_CommunityStatus]  DEFAULT ((0)) FOR [CommunityStatus]
    GO
    
    ALTER TABLE [dbo].[CommunityFollow] ADD  CONSTRAINT [DF_CommunityFollow_Status]  DEFAULT ('Following') FOR [Status]
    GO
    
    ALTER TABLE [dbo].[CommunityFollow] ADD  CONSTRAINT [DF_CommunityFollow_SendDate]  DEFAULT (getdate()) FOR [SendDate]
    GO
    
    

    Monday, February 8, 2016 11:46 AM

Answers

  • User-1716253493 posted

    Hi micah,

    insert query is look like : INSERT INTO TableName (col1,col2,col3) values (val1,val2,val3)

    insert query don't need WHERE clouse

    update quey is look like : UPDATE TableName Set col1=val1,col2=val2 WHERE @col3=val3

    Hi Micah,

    AFAIK you have 700+ posts, but never marking an answer

    Please mark someone answer, so i know which way you follow.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 2:32 AM
  • User-821857111 posted
    string getADPOST = "Insert INTO CommunityFollow  (MyUserName,FriendUserName,CommunityStatus) values (@MyUserName,@FriendUserName,1)";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 9:05 AM

All replies

  • User753101303 posted

    Hi,

    Your update statement is entirely wrong. It works this way: http://www.w3schools.com/SQl/sql_update.asp . Also see http://www.csharp-station.com/Tutorial/AdoDotNet/Lesson06 for how to use parameters (it seems you are in between now).

    So it could be something such as :

     string getADPOST = "Update CommunityFollow SET MyUserName=@UserName,CommunityStatus=1 WHERE UserId=@UserId";
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserName", Session["UserName"].ToString());
                    cmd.Parameters.AddWithValue("@UserId", Request.QueryString["Id"].ToString());
                    
    

    Add FriendUserName if needed. In your original code, it seems you didn't provide any value for this so I'm not sure what is your intent. Also it seems you are still confusing UserName and UserId as you had :

    cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());

    Then you could just call cmd.ExecuteNonQuery as here you don't read from the db.

    Monday, February 8, 2016 12:15 PM
  • User-2074858223 posted

    Sorry for the early error in my post , it is Insert query not update, i need to include this CommunityStatus=1 to insert true to the column, please can you rewrite your code to insert and include CommunityStatus to insert true

    ERROR

    Server Error in '/' Application.
    
    Incorrect syntax near 'MyUserName'.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'MyUserName'.
    
    Source Error: 
    
    
    Line 61:                 DataTable ds = new DataTable();
    Line 62: 
    Line 63:                 da.Fill(ds);
    Line 64:                  
    Line 65:                 }



    public void GetUserScraps(string username)
        {
    
    
            string str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            string getADPOST = "Insert INTO CommunityFollow  MyUserName,FriendUserName,CommunityStatus=1 WHERE MyUserName=@MyUserName AND FriendUserName=@FriendUserName";
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    cmd.CommandType = CommandType.Text;
                  //  cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
                    cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
                    cmd.Parameters.AddWithValue("@MyUserName", Request.QueryString["Id"].ToString());
                    cmd.Parameters.AddWithValue("@FriendUserName", Request.QueryString["Id"].ToString());
                    SqlDataAdapter da = new SqlDataAdapter(cmd);



    Monday, February 8, 2016 1:47 PM
  • User-1716253493 posted

    Hi micah,

    insert query is look like : INSERT INTO TableName (col1,col2,col3) values (val1,val2,val3)

    insert query don't need WHERE clouse

    update quey is look like : UPDATE TableName Set col1=val1,col2=val2 WHERE @col3=val3

    Hi Micah,

    AFAIK you have 700+ posts, but never marking an answer

    Please mark someone answer, so i know which way you follow.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 2:32 AM
  • User-2074858223 posted
    I mark answers but most times some of the answers are not applicable to me, So Why would I mark it when I haven't used them. If I apply your answer to my problem and it works then I mark yours.
    Tuesday, February 9, 2016 6:55 AM
  • User-2074858223 posted

    Am getting an error on the example

    CODE

     public void GetUserScraps(string username)
        {
    
    
            string str = ConfigurationManager.ConnectionStrings["CON"].ConnectionString;
            string getADPOST = "Insert INTO CommunityFollow  (MyUserName,FriendUserName,CommunityStatus=1) values (@MyUserName,@FriendUserName,@CommunityStatus=1)";
            using (SqlConnection con = new SqlConnection(str))
            {
                con.Open();
                using (SqlCommand cmd = new SqlCommand(getADPOST, con))
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    DataTable ds = new DataTable();
    
                    da.Fill(ds);
                    

    ERROR

    Server Error in '/' Application.
    
    Incorrect syntax near '='.
    
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.
    
    Source Error: 
    
    
    Line 58:                 DataTable ds = new DataTable();
    Line 59: 
    Line 60:                 da.Fill(ds);
    Line 61:                 
    Line 62:                 }

    I want to how to Insert true into CommunityStatus 

    Tuesday, February 9, 2016 7:42 AM
  • User-821857111 posted
    string getADPOST = "Insert INTO CommunityFollow  (MyUserName,FriendUserName,CommunityStatus) values (@MyUserName,@FriendUserName,1)";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 9, 2016 9:05 AM