|
Nice
I would like to add that you can often program against the IDbConnection and IDbCommand -interfaces here. One would only need to create a connection of the correct type, the CreateCommand factory could then be used to get a DbCommand of that type.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
Yep, I have been experimenting a bit with the interface-style DB operations, and it works well most of the time. IIRC I had to use real commands to get all the metadata I wanted, things such as GetSchema.
|
|
|
|
|
hello
i have a problem in this code
i don't enjoy the result in the texBox
System.Data.SqlClient.SqlDataReader affiche_titre;
System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand("SELECT libelle_titre FROM titre", ConnectBase.getinstance().Connection());
ListBox list = new ListBox();
DataTable titre = new DataTable();
affiche_titre = comm.ExecuteReader();
string [] row = null;
while (affiche_titre.Read())
{
row = new string[list.Items.Count];
for (int i = 0; i < list.Items.Count; i++)
{
row[i] = list.Items[i].ToString();
}
titre1.Text = row[0];
titre2.Text = row[1];
titre3.Text = row[2];
titre4.Text = row[3];
titre5.Text = row[4];
titre6.Text = row[5];
thans
|
|
|
|
|
For this code, it appears the problem is that you've never put anything into ListBox list !! So when you try to load the row array, the value of list.Items.Count is zero! and the loop is not executed (and I'd expect the titre1.Text = row[0]; line to throw an IndexOutOfRangeException ). It seems more likely that you wanted to use affiche_titre.FieldCount to fill the row array with the values from the reader's current row. I don't see ListBox list or DataTable titre used at all here (when corrected per my assumption).
If you've edited this to a smaller example then it is going to be difficult for anyone to give you good advice based on incomplete information. (We don't need the whole application!! just all of the relevant code. If you repost with more code, please use the code "button" above the Message entry box, to format the code more readably.)
|
|
|
|
|
hi
can anybody tell me how to save data to database using visual studio 2008(c#).
in sqlserver2005.
i had created the design in web application form.
please send me the code of save button.(c#)
|
|
|
|
|
shubham salwan wrote: please send me the code of save button.(c#)
We're not in the business of providing "free code". You are the programmer, your salary, you learn to write code to earn that.
If you're stuck, you can get hints and tips here. In this case, saving data to a database is usually done using an UPDATE statement in Sql. If that doesn't make any sense, then you'd best begin by finding a good book and reading up on the topic.
Bastard Programmer from Hell
if you can't read my code, try converting it here[^]
|
|
|
|
|
protected void btnSave_Click(object sender, EventArgs e)
{
string connString = "Data Source=ServerName;Initial Catalog=DbName;Integrated Security=True"; SqlConnection conn = new SqlConnection(connString);
conn.Open();
String sql = "INSERT INTO CustomerInfo(FirstName, LastName, Address, Order, Quantity) values ('Sai', 'Prasad', 'Hyderabad', 1, 100)";
SqlCommand cmd = new SqlCommand(sql,conn);
cmd.ExecuteNonQuery();
conn.Close();
}
|
|
|
|
|
Sure that works, but it is very very bad on several levels and showing it to a newbie will only create yet another newbie who doesn't write data access code properly and then has to come back and ask why things aren't working as expected.
0) You should put the actual DB access code in a Data Access Layer, then call the API of the DAL from the UI layer.
1) You should always use a parameterized query.
2) You should handle Exceptions.
|
|
|
|
|
the thing is that i use to make web sites in visual studio..
i m trying window application form.. thatz y facing problem
|
|
|
|
|
That shouldn't matter, a proper Data Access Layer can be called from many front-ends.
|
|
|
|
|
error occured
that
the type or namespace name sqlcommand could not be found
|
|
|
|
|
include below namespace in your projcet:
using System.Data.SqlClient;
|
|
|
|
|
|
Have a read of this using ADO.NET[^]
there are some simple tutorials in the article
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|
|
If the data you want to save is in a grid which is data-bound to a database table or view, or you're using EF or similar. you can just ask it to persist itself. Otherwise, you're going to have to construct a SQL query to store the data that you want. SQL Server 2005 doesn't have an equivalent of 'insert ... on duplicate key update' so doing a single query can be impossible.
Make sure you use a parameterised query, and handle failures to write gracefully.
|
|
|
|
|
hi,
i am using visual studio 2008
and making project in c#.
i had created web application form
but i am not able to connect it with database.
em using sqlserver2005 as database
shubhi
|
|
|
|
|
If you are having a problem with connections, this[^] would be your best bet.
|
|
|
|
|
What do you mean by "not able to connect it with database"?? What does you code look like that does the connection and query work?? What are the error messages you're getting?? Just saying "it doesn't work" doesn't help you.
|
|
|
|
|
i am not able o make connection con
|
|
|
|
|
shubham salwan wrote:
i am not able o make connection con
You do realize that we cannot see your code, or have your project available, or read your mind. If you expect to get help for your problems, you have to show the code that you are having trouble with and share the errors you are getting. We cannot possibly help you resolve issues if you do not give us the information that is needed to help you! Do you call your mechanic and tell him that your car is "broke" and expect that they should know eactly what you mean the problem is?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
string connString = "Data Source=shubhi-PC;Initial Catalog=employee;uid=sa;pass=abc;Integrated Security=True"; SqlConnection conn = new SqlConnection(connString);
conn.Open();
String sql = "INSERT INTO emp_detail(empid,empname,empadd,empsal) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
cmd.ExecuteNonQuery();
conn.Close();
textBox1.Text="";
i want to save data to sqlserver2005 ... bt itz not working
|
|
|
|
|
shubham salwan wrote: bt itz not working
Of course it's not working. Your connection string has a user name a password and you are telling it to use integrated security. Not to mention that you did not bother even defining a command object. You do realize that this won't even compile don't you?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Shhhhhhhh.... Don't tell him his code is subject to SQL Injection attacks either
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
|
|
|
|
|
I figure he has enough trouble without throwing gasoline on the bonfire.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Please stand in front of my pistol, smile and wait for the flash - JSOP 2012
|
|
|
|
|
Don't make your SQL statements that way -- use a parameterized query.
|
|
|
|