sql server - Connection string. Cannot implicitly convert 'String' to 'System.Data.SqlClient.SqlConnection' -


I find that error in the title.

My

  cmd.connection = conn;   

I have it

  public string Conn = connection.getConnection ();   

I am using a Windows application, how can I fix it?

This is my complete code

  using the system; Using System.Collections.Generic; Using System.ComponentModel; Using System.Data; Using System.Drawing; Using System.Linq; Using System.Text; Using System.Windows.Forms; Using System.Data.SqlClient; Name Location WindowsFormsApplication3 {Public Partial Classroom Room: Form {Public SqlDataReader Re; Public string Conn = connection.getConnection (); Public Cell () {InitializeComponent (); } Private Zero Room_Load (Object Sender, EventArgs E) {.. ..} Private Zero btnAdd_Click (Object Sender, EventArgs E) {try (if textbox1.Text.Length! = 0 & TextBox2.Text Length! = 0) {SqlCommand cmd = new SqlCommand (); Cmd.Connection = Conn; Cmd.CommandType = System.Data.CommandType.StoredProcedure; Cmd.CommandText = "insertToRoom"; Cmd.Parameters.AddWithValue ("@ room_name", textBox1); Cmd.Parameters.AddWithValue ("@ room_rate", text box 2); Cmd.ExecuteNonQuery (); } Other {message box. Show ("Please fill in all text box.", "Wrong input", message box button. OK, message box icon. Warning); }} Hold (Exception Pre) {Message Box. Show (ex.Message); }}}}}    

Try this way before web.conf or app. Get a connection string from conf and then create a sqlconnection to assign to sqlcommand.

  var connectionString = ConfigurationManager.ConnectionStrings ["NameofConstringFromWebconfig"]. ConnectionString; SqlCommand CMD = new SQL Commands (); Cmd.Connection = New SqlConnection (connectionString);    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -