I fill my <dx:ASPxTextBox> (DevExpress Control) using the "select" command from the server side with the following code(this is an example there are 20 fields approx):CustomerID.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString();
which is working as expected!
However, i want to update my table using the <asp:SqlDataSource> control with the same textboxes i use at the beggining as Control Parameters.When i give values to the textboxes in the .aspx page or like this exampletextbox.Text = "test"; the update command works. My conclusion is that i cannot update successfully my table because the textboxes get their values from an sqlcommand on the server side. Any ideas??? Am i doing something wrong?
Additional Code:
int customerUniqueID = 4;
string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM [Customers] WHERE [UniqueID] = #UniqueID", con); // table name
com.Parameters.Add("#UniqueID", SqlDbType.Int);
com.Parameters["#UniqueID"].Value = customerUniqueID;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
CustomerID.Text = ds.Tables[0].Rows[0]["CustomerID"].ToString();
CustomerName.Text = ds.Tables[0].Rows[0]["CustomerName"].ToString();
Details.Text = ds.Tables[0].Rows[0]["Details"].ToString();
SqlDataSource:
UpdateCommand="UPDATE [Customers] SET [CustomerName] = #CustomerName, [Details] = #Details WHERE [CustomerID] = 4">
Problem found... I had to remove the "select" code from Page_Load.... -.-
Related
I know this looks really simple but i've been looking for an answer for hours with no luck.
I want to fill my row values into a bunch of textboxes. How can I specify that [CompanyName] is going to be used by the companyName textbox? Please keep it as simple as possible (beginner level).
string customerUniqueID = "test";
string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM [Customers] WHERE [UniqueID] = #UniqueID", con); // table name
com.Parameters.Add("#UniqueID", SqlDbType.Int);
com.Parameters["#UniqueID"].Value = customerUniqueID;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
companyName.Text = ?????????
string customerUniqueID = "test";
string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
SqlConnection con = new SqlConnection(constr);
con.Open();
SqlCommand com = new SqlCommand("SELECT * FROM [Customers] WHERE [UniqueID] = #UniqueID", con); // table name
com.Parameters.Add("#UniqueID", SqlDbType.Int);
com.Parameters["#UniqueID"].Value = customerUniqueID;
SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds, "Customers");
companyName.Text = ds.Tables[0].Rows[0]["CompanyName"].ToString();
I will recommend some changes in your code:
Your sql query returning result from one set, so you can use DataTabe instead of DataSet.
To fill results from DB to your DataTable you can use SqlAdapter.Fill() method.
Use Field() generic method (more examples of Field()) to get values from your DataTable.
Use using blocks for disposable objects, or at least make sure you've closed them after.
There is no need of con.Open() to open connection when using Fill() method, because from MSDN:
The Fill method implicitly opens the Connection that the DataAdapter is using if it finds that the connection is not already open. If Fill opened the connection, it will also close the connection when Fill is finished. This can simplify your code when dealing with a single operation such as a Fill or an Update.
string customerUniqueID = "test";
string constr = ConfigurationManager.ConnectionStrings["SQLConnection"].ToString(); // connection string
using(SqlConnection con = new SqlConnection(constr))
{
SqlCommand com = con.CreateCommand();
com.CommandText = "SELECT * FROM [Customers] WHERE [UniqueID] = #UniqueID";
com.Parameters.Add("#UniqueID", SqlDbType.Int);
com.Parameters["#UniqueID"].Value = customerUniqueID;
using(SqlDataAdapter da = new SqlDataAdapter(com))
{
DataTable dt = new DataTable();
da.Fill(dt);
companyName.Text = dt.Rows[0].Field<string>("CompanyName");
}
}
Please feel free to comment, if I missed something.
I wanted to update a record to the database but it just keep reverting to its original value.
Below is my code. No error was display to me either.
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CandidateConnectionString"].ConnectionString);
conn.Open();
string updateData = "UPDATE Resume SET [Work_Experience] = #work_exp, [Educational_Level] = #edu_level, [Field_Of_Study] = #field_study, [University_Name] = #uni_name, [University_Location] = #uni_locate, [Graduation_Year] = #gra_year WHERE Cand_ID = (SELECT Cand_ID FROM Candidate WHERE Cand_Username = '"+ usernamelbl.Text +"')";
SqlCommand cmd = new SqlCommand(updateData, conn);
cmd.Parameters.AddWithValue("#work_exp", Work_Exp.Text);
cmd.Parameters.AddWithValue("#edu_level", Edu_Level.SelectedItem.Text);
cmd.Parameters.AddWithValue("#field_study", Field_Study.SelectedItem.Text);
cmd.Parameters.AddWithValue("#uni_name", Uni_Name.Text);
cmd.Parameters.AddWithValue("#uni_locate", Uni_Locate.Text);
cmd.Parameters.AddWithValue("#gra_year", Year.Text);
cmd.ExecuteNonQuery();
conn.Close();
Any Problem with the code?
I just found out a stupid mistake..
On the page_Load event, I have a line of code which is fetching value from the database
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CandidateConnectionString"].ConnectionString);
conn.Open();
string getdata = "SELECT * FROM Resume WHERE Cand_ID = (SELECT Cand_ID FROM Candidate WHERE Cand_Username = '" + usernamelbl.Text + "')";
SqlCommand com = new SqlCommand(getdata, conn);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sda.Fill(ds, "Resume");
Work_Exp.Text = ds.Tables["Resume"].Rows[0]["Work_Experience"].ToString();
Edu_Level.Text = ds.Tables["Resume"].Rows[0]["Educational_Level"].ToString();
Field_Study.Text = ds.Tables["Resume"].Rows[0]["Field_Of_Study"].ToString();
Uni_Name.Text = ds.Tables["Resume"].Rows[0]["University_Name"].ToString();
Uni_Locate.Text = ds.Tables["Resume"].Rows[0]["University_Location"].ToString();
Year.Text = ds.Tables["Resume"].Rows[0]["Graduation_Year"].ToString();
conn.Close();
But I didn't include it under if(!IsPostBack) and that's the reason whenever I submit, it overwrites my current value and revert it back to the original state. Thanks to you guys who trying to help me sort out and teaches me about new stuff. :)
The data binding works only when the page loads first time but it does not work otherwise. Somewhere in my page, i update and insert some new "Names" and i would like to show the newly added Names to be shown in the dropdownlist. But if i reload the page then the newly added Name will appear in the dropdownlist. How can i refresh the items in the dropdown? i thought my code should work. Pls help. thanks
private void RefreshDropDown()
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
SqlConnection con2 = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
SqlCommand cmd1 = new SqlCommand("SELECT DISTINCT [Name] FROM [Main] order by Name asc");
cmd1.Connection = con2;
con2.Open();
DropDownList1.DataSource = cmd1.ExecuteReader();
DropDownList1.DataTextField = "Name";
DropDownList1.DataValueField = "Name";
DropDownList1.DataBind();
con2.Close();
}
I assume that you have some kind of button to do insert of new names.
So on click of this button add call for RefreshDropDown() after you done inserting /updating your new names.
That should do the trick.
I am developing a webservice in which I am fetching some records with select statement based on status and then I need to change the status.
Its like:
select * from tblx where status='x'
and then
update tblx set status='y' where status='x' for the records fetched.
sample code:
string getpaid = "select top2 * from tblfameface where img_f_p='paid'";
con1 = new SqlConnection(conString1);
con1.Open();
SqlCommand cmd = new SqlCommand(getpaid, con1);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
da.Fill(dt1);
foreach (DataRow row in dt1.Rows)
{
string updatepaid = "update tblfameface set img_f_p='free' where img_f_p='paid' ";
con1 = new SqlConnection(conString1);
con1.Open();
SqlCommand cmd1 = new SqlCommand(updatepaid, con1);
int temp = cmd1.ExecuteNonQuery();
con1.Close();
}
}
I have no idea how to solve it.
Can I get any help??
Your Prototype is ok except your "Update" query.Rewrite your update query like
update tblx set status='y' where status=(select status from tblx where status='x');
As per your requirement:
string updatepaid = "update tblfameface set img_f_p='free' where img_f_p=(select top 2 img_f_p from tblfameface where img_f_p='paid')";
con1 = new SqlConnection(conString1);
con1.Open();
SqlCommand cmd1 = new SqlCommand(updatepaid, con1);
int temp = cmd1.ExecuteNonQuery();
con1.Close();
I hope it works for you.
Here Your update statement is wrong:
UPDATE <TABLE NAME> SET <Column> = <New Value> WHERE <Condition>
Here you can just execute a single update statement like below.
UPDATE YourTable
SET status='Y'
WHERE status='X'
Please, be more specific. Are you having problems with sql or c# ?
This could be helpful for C#:
http://msdn.microsoft.com/en-us/library/tyy0sz6b.aspx
and this for sql:
http://www.w3schools.com/sql/sql_update.asp
update tblx set status='y' where status='x';
I have a hyperlink which should print information from sql database.But I'm not able to know how to give that hyperlink value to sql query.
SqlConnection conn = new SqlConnection("Server=ILLUMINATI;" + "Database=DB;Integrated Security= true");
SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines= au_id", conn);
I want to get value au_id dynamically can anybody help me with this after clicking on the hyperlink.
Its like when i click on the headlines i should get the corresponding news.
First of all, you should use a LinkButton instead of a Hyperlink control as hyperlink redirects the page to a specified URL. But the LinkButton has a Click Event handler. On that click you can get the ID.
Your query will be look like...
SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = " + au_id, conn);
But It would be better if you use a Parameterized query to save yourself from a SQL Injection Attack.
SqlDataAdapter ADP = new SqlDataAdapter("select * from News where Headlines = #au_id", conn);
ADP.SelectCommand.Parameters.Add("#au_id", System.Data.SqlDbType.Int, 4, "au_id");
I'm not sure what do you exactly want, but here is simple example :
using ( command = new SqlCommand( ("select * from News where Headlines=" + au_id ), conn) //send au_id as string variable
{
DataTable outDT = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(outDT);
return outDT; // Your DataTable
}