locked
Data Not Insert Into Database RRS feed

  • Question

  • User1109811461 posted

    i convert my vb.net code into c# code after completing the convertion

    i run my system without any error but the data not insert in database

    could any one expertise here to look further my code...i do not know my code correct or not i'm new in this case hope can get help

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    
    public partial class RequestSupport : System.Web.UI.Page
    {
       
        protected void Drpbrowsertype_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (Drpbrowsertype.SelectedValue == "Others")
            { 
              lblothersbroweser.Visible=true;
              txtbrowsertype.Visible = true;
            }
            else
            lblothersbroweser.Visible = false;
            txtbrowsertype.Visible = false;
    
    
        }
    
    
        private SqlCommand FormCommand()
        {
            //string commandString = "INSERT INTO Support" + "(ProblemType,SystemType,Priority,BrowserType,OS,Date,ProDec)" + "VALUES(@ProblemType,@SystemType,@Priority,@BrowserType,@OS,@Date,@ProDec)";
            string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES(@ProblemType)";
            SqlCommand command = new SqlCommand(commandString);
    
    
            var _with1 = command.Parameters;
            _with1.Add("@ProblemType", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@SystemType", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@Priority", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@BrowserType", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@OS", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@Date", System.Data.SqlDbType.NVarChar, 50);
            //_with1.Add("@ProDec", System.Data.SqlDbType.NVarChar, 100);
    
    
           
            command.Parameters.AddWithValue("@ProblemType", drpProbtype.SelectedValue);
            //command.Parameters.AddWithValue("@SystemType", drpsysType.SelectedValue);
            //command.Parameters.AddWithValue("@Priority", drpPrio.SelectedValue);
            //command.Parameters.AddWithValue("@BrowserType", Drpbrowsertype.SelectedValue);
    
            //command.Parameters.AddWithValue("@OS", drpOs.SelectedValue);
            //command.Parameters.AddWithValue("@Date", txtdate.Text);
            //command.Parameters.AddWithValue("@ProDec", txtProDec.Text);
    
            return command;
    
        }
    
    
        protected void btnaddhard_Click(object sender, EventArgs e)
        {
            SqlCommand command = FormCommand();
            SqlConnection connection = SharedConnection.Connection;
          
    
            if ((!(connection.State == System.Data.ConnectionState.Open)))
            {
                connection.Open();
            }
    
            try
            {
                command.Connection = connection;
                command.ExecuteNonQuery();
    
            }
            catch (Exception ex)
            {
               
            }
            finally
            {
                connection.Close();
            }
        }
    }
    

    this is my share connection code

    using System.Configuration;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Diagnostics;
    using System.Data.SqlClient;
    
    public class SharedConnection
    {
    
    
        private static SqlConnection _connection;
        public static SqlConnection Connection
        {
            get
            {
                if (_connection == null)
                {
                    _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Default"].ConnectionString);
                }
    
                return _connection;
            }
        }
    
    }




    Monday, November 19, 2012 4:39 AM

Answers

  • User306743125 posted

    Hi.

    Do you need this part? I am not sure what it does.

            var _with1 = command.Parameters;
            _with1.Add("@ProblemType", System.Data.SqlDbType.NVarChar, 50);



    Try replacing the @ProblemType parameter, in your SQL query, with a value ... just to test the code (or put a watch on the variable)

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES('This is a test')";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 4:46 AM
  • User306743125 posted

    You can't refer to the dropdown from within your Sql query.
    You will have to do something like this...

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES @Value"
    commandString.Parameters.AddWithValue("@Value", drptype.selectedvalue);



     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 4:59 AM

All replies

  • User306743125 posted

    Hi.

    Do you need this part? I am not sure what it does.

            var _with1 = command.Parameters;
            _with1.Add("@ProblemType", System.Data.SqlDbType.NVarChar, 50);



    Try replacing the @ProblemType parameter, in your SQL query, with a value ... just to test the code (or put a watch on the variable)

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES('This is a test')";



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 4:46 AM
  • User1109811461 posted

    hi Basquiat, see you again

    the values should be from the control right such as dropdown list? so how it can be done..?

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES('drptype.selectedvalue')";

    is this code above correct..?

    base on your code above  the data is insert into database...but how to make the data that insert into database is depans on user input..?

    Monday, November 19, 2012 4:54 AM
  • User306743125 posted

    You can't refer to the dropdown from within your Sql query.
    You will have to do something like this...

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES @Value"
    commandString.Parameters.AddWithValue("@Value", drptype.selectedvalue);



     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 19, 2012 4:59 AM
  • User1109811461 posted

    hi basquiat

    is that my code is correct..? please see below i'm totaly new in c# code

     private SqlCommand FormCommand()
        {
            //string commandString = "INSERT INTO Support" + "(ProblemType,SystemType,Priority,BrowserType,OS,Date,ProDec)" + "VALUES(@ProblemType,@SystemType,@Priority,@BrowserType,@OS,@Date,@ProDec)";
            //string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES(@ProblemType)";
    
            string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES @Value";
    
           
            //string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES('This is a test')";
            SqlCommand command = new SqlCommand(commandString);
           
    
            var _with1 = command.Parameters;
            _with1.Add("@ProblemType", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@SystemType", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@Priority", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@BrowserType", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@OS", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@Date", System.Data.SqlDbType.NVarChar, 50);
            ////_with1.Add("@ProDec", System.Data.SqlDbType.NVarChar, 100);
    
            command.Parameters.AddWithValue("@Value", drpProbtype.SelectedValue);
           
            //command.Parameters.AddWithValue("@ProblemType", drpProbtype.SelectedValue);
            //command.Parameters.AddWithValue("@SystemType", drpsysType.SelectedValue);
            //command.Parameters.AddWithValue("@Priority", drpPrio.SelectedValue);
            //command.Parameters.AddWithValue("@BrowserType", Drpbrowsertype.SelectedValue);
    
            //command.Parameters.AddWithValue("@OS", drpOs.SelectedValue);
            //command.Parameters.AddWithValue("@Date", txtdate.Text);
            //command.Parameters.AddWithValue("@ProDec", txtProDec.Text);
    
            return command;
    
        }

    if correct still the data not insert into database ..please help me

    Monday, November 19, 2012 5:08 AM
  • User306743125 posted

    It appears correct.

    Although I am still not sure waht thisis for...

            var _with1 = command.Parameters;
            _with1.Add("@ProblemType", System.Data.SqlDbType.NVarChar, 50);



    Monday, November 19, 2012 5:11 AM
  • User1109811461 posted

    the code is from vb.net,,i just convert it into c# code

    so the  complete code should be like this

    string commandString = "INSERT INTO Support" + "(ProblemType)" + "VALUES @Value";
            SqlCommand command = new SqlCommand(commandString);
            command.Parameters.AddWithValue("@Value", drpProbtype.SelectedValue);

    is that correct..?

    Monday, November 19, 2012 5:14 AM
  • User306743125 posted

    yes that appears to be corrrect

    Monday, November 19, 2012 5:16 AM
  • User1109811461 posted

    hai Basquiat,

    finaly i got it ,i forget to put () on the values

    i attach to you the correct one for your future refences maybe, you resolve my issues

    thanks a lot's my expertiseCool glad to work with you..hope next time you can help me againLaughing

     string commandString = "INSERT INTO Support" + "(ProblemType,SystemType,Priority)" + "VALUES (@ProblemType,@SystemType,@Priority)";
            SqlCommand command = new SqlCommand(commandString);
            command.Parameters.AddWithValue("@ProblemType", drpProbtype.SelectedValue);
            command.Parameters.AddWithValue("@SystemType", drpsysType.SelectedValue);
            command.Parameters.AddWithValue("@Priority", drpPrio.SelectedValue);



    Monday, November 19, 2012 5:23 AM