This is my first time writing a database insert.
Working on my own and following other people examples I was able to write this code below.
using (SqlConnection openCon = new SqlConnection(""))
{
String connection = "INSERT INTO Damage (Area,Building,Conference,Apartment,Room,DescribeDamage) VALUES (@Area,@Building,@Conference,@Apartment,@Room,@DescribeDamage)";
using (SqlCommand CCC = new SqlCommand(connection, openCon))
{
try
{
openCon.Open();
CCC.Connection = openCon;
CCC.CommandType = CommandType.Text;
CCC.Parameters.Add("@Area", SqlDbType.Char, 24).Value = MainDDL.SelectedItem.ToString();
CCC.Parameters.Add("@Building", SqlDbType.Char, 9).Value = ddlBuildingCCC.SelectedItem.ToString();
CCC.Parameters.Add("@Conference", SqlDbType.Char, 5).Value = txtConferenceCCC.ToString();
CCC.Parameters.Add("@Apartment", SqlDbType.VarChar, 4).Value = txtApartmentCCC.ToString();
CCC.Parameters.Add("@Room", SqlDbType.VarChar, 10).Value = txtRoomCCC.ToString();
CCC.Parameters.Add("@DescribeDamage", SqlDbType.VarChar, 100).Value = txtDescribeCCC.ToString();
CCC.ExecuteNonQuery();
CCC.Dispose();
}
catch (SqlException)
{
// error here
}
finally
{
openCon.Close();
}
}
}
Updated code from Guffa below, i am going to rewrite according to your comments but i want to see if i can get this to work first.
using (SqlConnection connection = new SqlConnection("Asset managementConnectionString"))
{
String query = "INSERT INTO Damage (Area,Building,Conference,Apartment,Room,DescribeDamage) VALUES (@Area,@Building,@Conference,@Apartment,@Room,@DescribeDamage)";
using (SqlCommand CCC = new SqlCommand(query, connection))
{
try
{
connection.Open();
CCC.CommandType = CommandType.Text;
CCC.Parameters.Add("@Area", SqlDbType.Char, 24).Value = MainDDL.SelectedItem.ToString();
CCC.Parameters.Add("@Building", SqlDbType.Char, 9).Value = ddlBuildingCCC.SelectedItem.ToString();
CCC.Parameters.Add("@Conference", SqlDbType.Char, 5).Value = txtConferenceCCC.ToString();
CCC.Parameters.Add("@Apartment", SqlDbType.VarChar, 4).Value = txtApartmentCCC.ToString();
CCC.Parameters.Add("@Room", SqlDbType.VarChar, 10).Value = txtRoomCCC.ToString();
CCC.Parameters.Add("@DescribeDamage", SqlDbType.VarChar, 100).Value = txtDescribeCCC.ToString();
CCC.ExecuteNonQuery();
}
catch (SqlException)
{
throw; // just rethrow for now
}
}
}
Response.Redirect("~/Default.aspx");
}
}
I lost the first example i was following along with and haven't been able to find it again. So I think i am trying two combine two different methods by accident.
Problem: Right now nothing is debugging but i haven't had anything insert into the database. I think some of the code isn't needed, can you help me compact the code and getting the data inserting? I am to the point of rewriting the whole thing again following a different example. But i would like to know why this isn't working for experience.
UPDATED Problem, Figured out why me error where not getting reported even when i was throwing them. Right now i am getting
Format of the initialization string does not conform to specification starting at index 0. on using (SqlConnection connection = new SqlConnection("AssetmanagementConnectionString"))
I haven't been able to figure out the issue. What are the steps i can take to figure this out?
Disposewithin yourusingblock. It'll do that for youtxtConferenceCCCetc - is that astring? or aTextBox? or...?Executemethod): pastie.org/8749839 - note the lack of lots ofDispose(),Close(),try,catch,finally- all of that is redundant and causing you problems