Hi.
I'm developing a code where it will store temporary data into gridview from textbox,and the value will have a link to google search.
Now i want to save the values to database,according to appropriate table column that matches the gridview value.
Any ideas guys?
Here's the code :
I use the back code as a script.
9 3693
Ok?
It looks like you're doing everything fine except that your SQL Insert Into statement isn't actually doing anything.
You need to loop through each row in your DataTable and call the Insert Into statement for that row/record. You currently are not doing that.
Also, it is not good to insert user-provided values directly into your SQL query because it leaves you open to a SQL Injection Attack. Instead of adding the values to be inserted into your database by concatenating them into the string that contains your SQL command, use parameters.
Please review the following article that has an example of how to use Parameters: Database tutorial Part 1
-Frinny
I just want to know the method on how to get the temporary values in the gridview and then save to database.
Loop through each DataRow in the DataTable.Rows, access the "name" field.
Like this: -
// For each row, retrieve the value in the name field.
-
foreach(DataRow myRow in dt.Rows){
-
string name = myRow("name");
-
//......
-
}
-
-Frinny
Hi.
I try to apply your code by using parameter.
But it gives me error.
Here's the cs code : -
protected void BtnSendToDatabase_Click(object sender, EventArgs e)
-
{
-
foreach (DataRow row in dt.Rows)
-
{
-
Console.WriteLine(row["name"].ToString());
-
}
-
-
SqlCommand cmd = new SqlCommand();
-
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
-
cmd.Parameters.AddWithValue("@compName", TxtName.Text);
-
-
foreach (DataRow row in dt.Rows)
-
{
-
cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
-
cmd.ExecuteNonQuery();
-
}
-
-
cmd.Dispose();
-
conn.Close();
-
conn.Dispose();
-
}
-
But i'm getting this error:
CS1061: 'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Parameter' and no extension method 'Parameter' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found (are you missing a using directive or an assembly reference?)
Source error:
cmd.Parameter.AddWithValue("@compName", row["name"].ToString());
I tried to use command instead of cmd,but it still gives error.
Any ideas?
If use the "Add" method instead and set the parameter in the loop. You are trying to add the @compName parameter more than once.
-Frinny
Sorry,but i dont get you..
Just call the Add method once.
Do not call it in the loop. - protected void BtnSendToDatabase_Click(object sender, EventArgs e)
-
{
-
foreach (DataRow row in dt.Rows)
-
{
-
Console.WriteLine(row["name"].ToString());
-
}
-
-
SqlCommand cmd = new SqlCommand();
-
cmd.CommandText = "INSERT INTO Table1 (Company_Name) VALUES (@compName)";
-
cmd.Parameters.Add("@compName", SqlDbType.VarChar);
-
-
foreach (DataRow row in dt.Rows)
-
{
-
cmd.Parameters.Item["@compName"].Value = row["name"].ToString();
-
cmd.ExecuteNonQuery();
-
}
-
-
cmd.Dispose();
-
conn.Close();
-
conn.Dispose();
-
}
How about the connection string?
You could provide the connection string to the SqlCommand constructor so that the SqlCommand is initialized with it. It might help you to look over the MSDN documentation on the SqlCommand Class.
-Frinny
Sign in to post your reply or Sign up for a free account.
Similar topics
by: SOAP |
last post by:
how to save binary data to database by using CMP2.0?
I would like to save a save image to database through EJB. but I don't know
which data type shoud I use.
any examples?
thanks a lot
|
by: joe |
last post by:
Can i save a session to database
|
by: Irene |
last post by:
Hello all!
I'm creating a web site in ASP.NET (VB.NET). One of the requirements was to
allow users to create orders going through several steps. A must have is to
have an option to save the work...
|
by: craig dunn |
last post by:
I've used the code from the post for a while now... but recently upgraded to SQL Server 2005.
I've written a new script for SQL Server 2005, which can be found at...
|
by: cgd |
last post by:
how to do
Save Word document to Database in memory,
not through the disk
|
by: Ashu Saxena |
last post by:
Hi !
I'm a php developer.
I'm developing a site into which I have to upload a text file and a image file
from users through a browse box and then I have to retrieve that file and save it
into...
|
by: slapshock |
last post by:
hi every one....
i am new in c#.net....
i want to know on how to upload images and save it to the database...
please give me the exact code on how to do it....
i need the code in doing my...
|
by: plusplus |
last post by:
Hi,
How can i save datagrid contents to database?
Database i'm using is MySQL.
Thanks lots! (:
|
by: raamay |
last post by:
hey experts, please advise me what is the best way to save multiple checkboxes value in a database. I have 6 checkboxes and i came across storing the values in a single column of a table which i dont...
|
by: omerbutt |
last post by:
hi i have an applicationa for online test in there i have to save some questions in the db now when i try to save this question
what is 2 + 2 ?
when i save it in the database it removes the +...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |