SQL Insert - How To Combine More Than One Field Value To A Single Column Insert
Jan 18, 2010
I have a form with many form fields and controls, with some offering an "other" if a value does not meet the needs for the user. How can I bind the sql insert so that it will take the ddl selection along with the txt field selection for the "other" value? Even if the txt field is empty it should not be a big deal since the ddl would provide something other than null. Since the column in the db does not allow nulls. I'm using asp.net (vb) 3.5 sp1
i use this to select one checkbox to isselected column how i convert this to multi checkboxlist to single column i use many ways more than 3 days without success
private void BindCheckBoxList() { DataTable dt = new DataTable(); SqlConnection connection = new SqlConnection(GetConnectionString()); try { connection.Open(); string sqlStatement = "SELECT * FROM boby"; SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); if (dt.Rows.Count > 0) { CheckBoxList1.RepeatColumns = 4; // set the number of columns in the CheckBoxList CheckBoxList1.DataSource = dt; CheckBoxList1.DataTextField = "Name"; // the items to be displayed in the list items CheckBoxList1.DataValueField = "Name"; // the id of the items displayed CheckBoxList1.DataBind(); //Setting the Selected Items in the ChecBoxList based from the value in the database //to do this, lets iterate to each items in the list for (int i = 0; i < dt.Rows.Count; i++) { if (!string.IsNullOrEmpty(dt.Rows[i]["IsSelected"].ToString())) { CheckBoxList1.Items[i].Selected = Convert.ToBoolean(dt.Rows[i]["IsSelected"]); } } } } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } private void Update(string name, bool isSelected) { SqlConnection connection = new SqlConnection(GetConnectionString()); SqlCommand cmd; string sqlStatement = string.Empty; try { connection.Open(); sqlStatement = "UPDATE handymen SET IsSelected = @IsSelected WHERE Name = @BizName"; cmd = new SqlCommand(sqlStatement, connection); cmd.Parameters.AddWithValue("@Name", name); cmd.Parameters.AddWithValue("@IsSelected", isSelected); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Insert/Update error"; msg += ex.Message; throw new Exception(msg); } finally { connection.Close(); } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindCheckBoxList(); } } protected void Button1_Click(object sender, EventArgs e) { string str = string.Empty; for (int i = 0; i < CheckBoxList1.Items.Count; i++) { if (CheckBoxList1.Items[i].Selected) { str = CheckBoxList1.Items[i].Text; Update(str, CheckBoxList1.Items[i].Selected); } } //ReBind the List to retain the selected items on postbacks BindCheckBoxList(); }
I am using Visual studio 2010 express and I have a details view databinded to an sql database table. When I use the Insert option on the view it causes an error due to it trying to insert a null into the primary key field. If the Insert function does not create a new primary key entry how is it created?
I am doing a data warehouse project.I have two tables tblA (id, type) and tblB(city, no_crimes, type).I want to create (insert) a number of rows based on the value of no_crimes.For Example, in tblB(Leeds, 2000, murder). SO, I need to insert 2000 rows into tblA by a single INSERT statement (not 2000 statements).
insert destinationtable(col1,col2,col3) select col,col2,col3 from sourcetable t1 where not exists(select * from destinationtable t2 where t1.col1 = t2.col1)
I would like to insert into destinationtable which is MS SQL and the sourcetable is Oracle. Is there a way to do this?
i am developed on simple user registeration application in that i gave address field .when i am trying to enter something like this (1st main,b'lore) but wont take while inserting.
my query is
sqlcommand cmd=new sqlcommand("insert into registeration_form(name,address)values('"+txtname.text+"','"+txt_address+"')",con);
how can i pass the quotes also to my query if any one know reply me.
How can I insert value in two tables by single sql query because if I'm using two sql query it gives problem me on binding data and connection. Like I have 2 table table 1, table 2 ... In table I'm only save id and name and in other table I'm saving id, time1, time2
I am facing a situation wherein i have to ask the user for the files to upload and save them as blob in the database. I have a single file upload control on the page and user can add as many files as he wants. When the user browse the file and click add button then the file gets added at some temporary location on the page and later on when the user has specified all the files to upload then I would like to insert all these files using a single insert query in the database.
I have three textbox ... textbox1 and textbox2 and textbox3 I want when I choose from date in textbox1 say 1-May-2011 and to date in textbox2 say 30-May-2011 and in textbox I type 1,2,3,4,5 I want on button click ... event the values will be entererd in database from 1-May-2011 to 30-May2011 as : mentioned below :
DATABASE STRUCTURE ID Date Items 1 1-May-2011 1,2,3,4,5 2 2-May-2011 1,2,3,4,5 3 3-May-2011 1,2,3,4,5
so on till 30_may-2011 Record will be inserted in database according to from date and to date choosen in textbox1 and textbox2 respectively.
I am developing a mini project in asp.net-c#..how can i visible number of textboxes at runtime to insert records by single click in sql server 2005. for example. If I have assigned 3 subjects in Assign_Subject table then i want 3 textbox at runtime but when we retrieves the subjects from Assign_Subject table we don't know the exact number of subjects to insert students marks into Exam_Result Table....
I need to create textboxes dynamically as per assigned subjects and also I need to read the text entered later (in my case its marks of the student)then 1 Insert button to insert all subjects marks as per single click.
I am trying to retrieve the ID (auto incremented) form a Table after I do an insert. I have tried using one of the Variable I used to insert with no success. I am inserting a record with Todays Date (if one does not allready exsist) then trying to read that record to get the AutoInc number (DailyTimeRecordID). Where am I going wrong?
I am new to .NET and MVC, so this is most likely a simple question: How can I have a default value appear for an Html.TextBox or TextArea field, for example if the code is currently - <%=Html.TextBox("ApplyToFax", Model.Competition.ApplyToFax, New With {.class = "regular", .maxlength = "50"})%>. I am looking to have a default fax number appear in the box when the page loads, for example (999) 999-9999.
For example: INSERT INTO table (col1, col2, col3) VALUES ('value1','value2,'value3')But I would only like to insert the row if value3 isnt already present in col3 in some previous row. What is the best way to do that?And the insert command is run 50+ times in a loop (with different values each time for value3)
I am trying to pass in as an insert parameter for SQLds2 the scope_identity of the insert of SQLds1.
Exception Details: System.InvalidOperationException: Error executing 'InsertCommand' in SqlDataSource 'SqlDataSource1'. Ensure the command accepts the following parameters: @Name1, @NewId
Getting the message that ObjectDataSource ... count not find a non-generic method ... that has parameters ... The parameter list is fine until I get to the very end and it list the two data fields that are in my DetailsView. Using the ASP Tutorial #17 (Examining the Events Associated with Inserting, Updating, and Deleting) and the Exploring the Data Modification - specifically the one showing the INSERT of the Product Name and UnitPrice. ASPX page is pretty simple and attempting something similar, two fields to insert a new record - update the other fields later.
Full Error Message follows along with ASPX and the AsignaturasBLL.cs. Interesting thing is that when I swap the two fields in the DetailView, then the last two fields in the error message are swapped. In the error message I bolded these two fields.