Author |
Message |
Zentik

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
well here is my code of ISERT in C# ===========================================
InitializeComponent(); SqlConnection coneccion; coneccion = new SqlConnection("Data Source=sagara\\sqlexpress;" + "Trusted_Connection=yes;" + "database=Hospital; "); SqlCommand orden;
orden = new SqlCommand(q, coneccion);
orden.Parameters.Add(new SqlParameter("txtAPELLIDO", SqlDbType.Text));
orden.Connection.Open(); orden.ExecuteNonQuery(); orden.Connection.Close();
===========================================
my problem is that: it is inserting values to the "table" but they are null or empty values. i don’t now why.
txtNOMBRE and txtAPELLIDO are 2 different textbox
but if y put direct values in, they are inserting correctly.... but it cannot catch what i’m writing in the textbox
(sorry for my enlgish) hope someone can help me
.NET Development14
|
|
|
|
 |
one_pom

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
Try
string q = "INSERT INTO prueba (nombre, apellido) VALUES ( txtNOMBRE.Text, txtAPELLIDO.Text)";
If that doesn't work try
string q = "INSERT INTO prueba (nombre, apellido) VALUES ( txtNOMBRE.Text.ToString(), txtAPELLIDO.Text.ToString() )";
Just incase your inserting digits or special characters. Pom.
|
|
|
|
 |
Jeff Wharton

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
Try using code like this:
SqlConnection coneccion; coneccion = new SqlConnection("Data Source=sagara\\sqlexpress;" + "Trusted_Connection=yes;" + "database=Hospital; "); SqlCommand orden; string q =
; orden = new SqlCommand(q, coneccion); orden.Parameters.Add(new SqlParameter(
, this.txtNOMBRE.Text)); orden.Parameters.Add(new SqlParameter("txtAPELLIDO", this.txtAPELLIDO.Text)); orden.Connection.Open(); orden.ExecuteNonQuery(); orden.Connection.Close();
Cheers Jeff
|
|
|
|
 |
Lepaca

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
You have to declare the size of the parameter...
50)).Value = this.txtNOMBRE.Text;
50 is only an example. See your column properties
|
|
|
|
 |
VMazur

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
How are your columns declared in a database If they are declared as CHAR or VARCHAR, then you also need to use this type for your parameters and specify size of these parameters. Text type may not work properly in your case if you have different type in a database, because Text is a BLOB-type that is not the same as strings. Your parameters would look like (check proper size for them)
orden.Parameters.Add(new SqlParameter("txtAPELLIDO", SqlDbType.VarChar, 100));
|
|
|
|
 |
Zentik

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
thanks to yoy, i found this too and works.. ================================================================
SqlConnection sqlConnection = new SqlConnection("initial catalog=prueba;data source=sagara\\sqlexpress;" + "Trusted_Connection=yes;" + "database=Hospital; "); SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
sqlConnection.Open(); try{ sqlCommand.ExecuteNonQuery(); } catch(Exception exc){ throw new Exception("Error al insertar datos", exc); } finally{ sqlConnection.Close(); }
================================================================
and is the same code to capute or read data from CHECKBOX or LISTBOX too or what i must change
thanks
|
|
|
|
 |
Lepaca

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
Yes, you can use everything is a string...
|
|
|
|
 |
Zentik

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
well, im working with an sql server database, inserting files here i dont have problems. and i’m trying to insert int an SQL DB File (.mdf) and i’m getting an error: something like this, I DONT HAVE REMOTE PERMISSON or something like this.... s (sorry for my english)
|
|
|
|
 |
MasterG

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
This kind of errors may cause from your sql server properties. You have to set remote connections property both local and remote. You can make this setting from "SQL server surface are configuration" section which may already installed to your machine with SQL express(correct me if i'm wrong.) You can also change this setting from Sql Server Configuration Manager. If you want more information pls let me know.
Happy Coding....
|
|
|
|
 |
Zentik

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
No se controlo System.Data.SqlClient.SqlException Message="An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)" Source=".Net SqlClient Data Provider" ErrorCode=-2146232060 Class=20 LineNumber=0 Number=-1 Server="" State=0
this is my code
SqlConnection sqlConnection = new SqlConnection("initial catalog=prueba;data source=c\\pruebas.mdf;" + "Trusted_Connection=yes;"); SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);
sqlConnection.Open();
try { sqlCommand.ExecuteNonQuery(); } catch (Exception exc) { throw new Exception("Error al insertar datos", exc); } finally { sqlConnection.Close(); }
and in sql configuration y put in Remote connection : LOCAL AND REMOTE CONNECTION-> using both tcp/ip and named pipes
|
|
|
|
 |
MasterG

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
I think you have something strange in your connectionstring. Once i got this error, i remembered , you need to write your data source in correct form. In your string your sql server instance is not in correct formation. Please correct that. Hey and this one may be usefull: When connecting to your sql server you may use \\Server\Instance or Server\Instance . One of this two can take you to correct solution.
1)Correct your conStr(like your first post).
2)Sure that Remote connections avaliable.
3)Try to write your sql server name and instance like i gave.
I hope this tips may help. Happy coding.
|
|
|
|
 |
Jeff Wharton

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
In C#, using a "\" in a string denotes using a special escape character (literal character); therefore C# would look at "server\instance" and think that you are trying to use escape character "\i" which is invalid.
|
|
|
|
 |
MasterG

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
That ok. Every C# programmer must know this. If rewrite my post; try to connect your sql server using server explorer. In server name section write your server name try to connect. (this post is more complicated ha :) )
|
|
|
|
 |
Zentik

|
Posted: .NET Framework Data Access and Storage, Problem inserting data to SQL |
Top |
well i have allmost complete my program, my question is how do I export the database to another computer to mark work my program
because it says cannot connect to "Sagara\Gues" even i have the datasource=(local)\sqlexpress
|
|
|
|
 |
|