I need help on determining what cause the error of my code.
"Conversion failed when converting the varchar value 'Undergraduate' to data type int." when i hit the ADD Button.
I'm struggling to find what went wrong. It worked at first but suddenly, the error alway occur.
This is the full code;
private void btnadd_Click(object sender, EventArgs e)
{
using (SqlConnection conncurr = new SqlConnection(Properties.Settings.Default.connectionstring))
using (SqlCommand cmdcurr = new SqlCommand("curriculumadd", conncurr))
{
try
{
conncurr.Open();
var newaccountparam1 = new SqlParameter("#dccode", SqlDbType.VarChar);
var newaccountparam2 = new SqlParameter("#dcdesc", SqlDbType.VarChar);
var newaccountparam3 = new SqlParameter("#currtitle", SqlDbType.VarChar);
var newaccountparam4 = new SqlParameter("#units", SqlDbType.Int);
var newaccountparam5 = new SqlParameter("#labunits", SqlDbType.Int);
var newaccountparam6 = new SqlParameter("#lecunits", SqlDbType.Int);
var newaccountparam7 = new SqlParameter("#yearlevel", SqlDbType.Int);
var newaccountparam8 = new SqlParameter("#sem", SqlDbType.Int);
var newaccountparam9 = new SqlParameter("#labhrs", SqlDbType.Int);
var newaccountparam10 = new SqlParameter("#lechrs", SqlDbType.Int);
var newaccountparam11 = new SqlParameter("#acadlevel", SqlDbType.Int);
var newaccountparam12 = new SqlParameter("#subjcode", SqlDbType.VarChar);
var newaccountparam13 = new SqlParameter("#subjdesc", SqlDbType.VarChar);
var newaccountparam14 = new SqlParameter("#subjcat", SqlDbType.VarChar);
var newaccountparam15 = new SqlParameter("#curreffectvty", SqlDbType.VarChar);
newaccountparam1.Value = txtcoursecode.Text;
newaccountparam2.Value = txtdcourse.Text;
newaccountparam3.Value = currtitle;
newaccountparam4.Value = totalunits;
newaccountparam5.Value = txtlabunits.Text;
newaccountparam6.Value = txtlecunits.Text;
newaccountparam7.Value = yearlevel;
newaccountparam8.Value = semestr;
newaccountparam9.Value = labhrs;
newaccountparam10.Value = lecthrs;
newaccountparam11.Value = acadlevel;
newaccountparam12.Value = txtsubjcode.Text;
newaccountparam13.Value = txtdesctitle.Text;
newaccountparam14.Value = cmbsubjfield.Text;
newaccountparam15.Value = txteffectivity.Text;
cmdcurr.Parameters.Add(newaccountparam1);
cmdcurr.Parameters.Add(newaccountparam2);
cmdcurr.Parameters.Add(newaccountparam3);
cmdcurr.Parameters.Add(newaccountparam4);
cmdcurr.Parameters.Add(newaccountparam5);
cmdcurr.Parameters.Add(newaccountparam6);
cmdcurr.Parameters.Add(newaccountparam7);
cmdcurr.Parameters.Add(newaccountparam8);
cmdcurr.Parameters.Add(newaccountparam9);
cmdcurr.Parameters.Add(newaccountparam10);
cmdcurr.Parameters.Add(newaccountparam11);
cmdcurr.Parameters.Add(newaccountparam12);
cmdcurr.Parameters.Add(newaccountparam13);
cmdcurr.Parameters.Add(newaccountparam14);
cmdcurr.Parameters.Add(newaccountparam15);
cmdcurr.CommandType = CommandType.StoredProcedure;
cmdcurr.ExecuteNonQuery();
}
finally
{
conncurr.Close();
conncurr.Dispose();
userlogsmonitor.ActiveForm.Refresh();
}
}
}
Code for converting string to integer;
private void cmbacadlevel_SelectedIndexChanged(object sender, EventArgs e)
{
switch (cmbacadlevel.SelectedItem.ToString().Trim())
{
case "Undergraduate":
acadlevel = 1;
break;
case "Masteral":
acadlevel = 2;
break;
case "Doctorate":
acadlevel = 3;
break;
case "Senior High School":
acadlevel = 4;
break;
}
I tried editing the code but it seems not lucky enough. Can you please scrutinize my code and try to give an idea on how to deal with it?
Verify following things
Order and type of parameter is same as in the stored procedure
Either use breakpoint to verify values of int parameters
Or use SQL Server Profiler to to check if int parameters have valid values.
Related
I am inserting data from datagridview into database , I debug my code and found the error Input string was not in a correct format. in the following line
Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString())
this column is an integer column and when i replaced this line with integer value for example number 3 insert completed without errors , and the other integer columns also inserted same way without any error
this is my code :
if (checkApproveResult.Checked == false && chkupdateApproved.Checked == false)
{
for (int i = 0; i < dgvResult.Rows.Count; i++)
{
result.UPDATE_LAB_RESULTS(Convert.ToInt32(txtOrder.Text),
dgvResult.Rows[i].Cells[7].Value.ToString(),
5,
dgvResult.Rows[i].Cells[6].Value.ToString(),
txtExamUser.Text,
DateTime.Parse(DateTimeExamined.Value.ToString()),
Convert.ToInt32(dgvResult.Rows[i].Cells[2].Value),
dgvResult.Rows[i].Cells[4].Value.ToString(),
dgvResult.Rows[i].Cells[5].Value.ToString(),
Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString()),
Convert.ToInt32(txtPno.Text),
Convert.ToInt32(txtcustid.Text),
txtReqForm.Text,
dgvResult.Rows[i].Cells[1].Value.ToString(),
Convert.ToInt32(dgvResult.Rows[i].Cells[8].Value.ToString()));
}
MessageBox.Show("Result Saved Successfully ", "Entering Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (chkupdateApproved.Checked == true)
{
for (int i = 0; i < dgvResult.Rows.Count; i++)
{
result.ADD_LAB_RESULTS_UPDATES(Convert.ToInt32(txtsampleid.Text),
Convert.ToInt32(txtOrder.Text),
Convert.ToInt32(dgvResult.Rows[i].Cells[2].Value),
Convert.ToInt32(txtgroupid.Text),
"YES",
6,
dgvResult.Rows[i].Cells[11].Value.ToString(),
DateTime.Parse(DateTimeExamined.Value.ToString()),
dgvResult.Rows[i].Cells[13].Value.ToString(),
DateTime.Parse(dateTimeApprove.Value.ToString()),
dgvResult.Rows[i].Cells[4].Value.ToString(),
dgvResult.Rows[i].Cells[5].Value.ToString(),
dgvResult.Rows[i].Cells[6].Value.ToString(),
Convert.ToInt32(txtpackageid.Text),
Convert.ToInt32(dgvResult.Rows[i].Cells[9].Value.ToString()),
2,
Convert.ToInt32(txtPno.Text),
Convert.ToInt32(txtcustid.Text), txtReqForm.Text,
Convert.ToInt32(dgvResult.Rows[i].Cells[8].Value.ToString()),
txtupdatedby.Text,
DateTime.Parse(dateupdate.Value.ToString()));
}
update.UPDATE_LAB_RESULT_STATUS(Convert.ToInt32(txtOrder.Text), Convert.ToInt32(txtsampleid.Text), 2);
MessageBox.Show("Result Updated Successfully ", "Update Result", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
And the code for ADD_LAB_RESULT_UPDATES was :
public void ADD_LAB_RESULTS_UPDATES(int SAMPLE_ID, int ORDER_ID,int TESTID,int GROUPID, string NORMAL_RESULT,
int SAMPLE_STATUS,string EXAMINED_BY,DateTime EXAMINED_DATE, string APPROVED_BY, DateTime APPROVED_DATE,
string RESULT_NUMBER, string RESULT_REPORT, string RESULT_NOTE,int packageid, int machine_id, int deptid,
int patient_no, int custid, string REQ_FORM_NO,int serial,string UPDATED_BY,DateTime UPDATED_DATE)
{
DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
DAL.open();
SqlParameter[] param = new SqlParameter[22];
param[0] = new SqlParameter("#SAMPLE_ID", SqlDbType.Int);
param[0].Value = SAMPLE_ID;
param[1] = new SqlParameter("#ORDER_ID", SqlDbType.Int);
param[1].Value = ORDER_ID;
param[2] = new SqlParameter("#TESTID", SqlDbType.Int);
param[2].Value = TESTID;
param[3] = new SqlParameter("#GROUPID", SqlDbType.Int);
param[3].Value = GROUPID;
param[4] = new SqlParameter("#NORMAL_RESULT", SqlDbType.VarChar, 10);
param[4].Value = NORMAL_RESULT;
param[5] = new SqlParameter("#SAMPLE_STATUS", SqlDbType.Int);
param[5].Value = SAMPLE_STATUS;
param[6] = new SqlParameter("#EXAMINED_BY", SqlDbType.VarChar, 50);
param[6].Value = EXAMINED_BY;
param[7] = new SqlParameter("#EXAMINED_DATE", SqlDbType.DateTime);
param[7].Value = EXAMINED_DATE;
param[8] = new SqlParameter("#APPROVED_BY", SqlDbType.VarChar, 50);
param[8].Value = APPROVED_BY;
param[9] = new SqlParameter("#APPROVED_DATE", SqlDbType.DateTime);
param[9].Value = APPROVED_DATE;
param[10] = new SqlParameter("#RESULT_NUMBER", SqlDbType.VarChar, 50);
param[10].Value = RESULT_NUMBER;
param[11] = new SqlParameter("#RESULT_REPORT", SqlDbType.VarChar, 2000);
param[11].Value = RESULT_REPORT;
param[12] = new SqlParameter("#RESULT_NOTE", SqlDbType.VarChar, 200);
param[12].Value = RESULT_NOTE;
param[13] = new SqlParameter("#packageid", SqlDbType.Int);
param[13].Value = packageid;
param[14] = new SqlParameter("#machine_id", SqlDbType.Int);
param[14].Value = machine_id;
param[15] = new SqlParameter("#deptid", SqlDbType.Int);
param[15].Value = deptid;
param[16] = new SqlParameter("#patient_no", SqlDbType.Int);
param[16].Value = patient_no;
param[17] = new SqlParameter("#custid", SqlDbType.Int);
param[17].Value = custid;
param[18] = new SqlParameter("#REQ_FORM_NO", SqlDbType.VarChar, 50);
param[18].Value = REQ_FORM_NO;
param[19] = new SqlParameter("#serial", SqlDbType.Int);
param[19].Value = serial;
param[20] = new SqlParameter("#UPDATED_BY", SqlDbType.VarChar, 50);
param[20].Value = UPDATED_BY;
param[21] = new SqlParameter("#UPDATED_DATE", SqlDbType.DateTime);
param[21].Value = UPDATED_DATE;
DAL.ExecuteCommand("ADD_LAB_RESULTS_UPDATES", param);
DAL.close();
}
the error with parameter 14 machine_id
also the table in the database machine_id int.
What is the error ?
More information I have first if statement (update statement)
result. UPDATE_LAB_RESULTS includes same parameter machine_id and its working and inserting without errors.
Second if statement (insert statement) and show the error with machine_id parameter.
You have to check the following :
1- check the parameter list variables types (string , int , ...) and compare it with your procedure you will find one variable type different in the stored procedure integer and in the parameter list string.
I am trying to get a little tool to work, for our little company.
This should fill data from a DataGridView into a SQL Server Standard database.
Reading out the SQL table is working fine, but I can't get the tool to write the values.
I get the error mentioned in the title.
Here the code:
var kdNummer = new SqlParameter("Kundennummer", SqlDbType.Int);
var kdName = new SqlParameter("Kundenname", SqlDbType.VarChar);
var kdMail = new SqlParameter("Kundenmail", SqlDbType.VarChar);
var kdTele = new SqlParameter("Telefon", SqlDbType.VarChar);
string kdquery = "INSERT INTO Kunden VALUES (#Kundennummer, #Kundenname, #Kundenmail, #Telefon)";
using (SqlConnection updatedb = new SqlConnection("Data Source=;Initial Catalog=updatedb;User ID=;Password="))
{
updatedb.Open();
for (int i = 0;i<dataGridView1.Rows.Count;i++)
{
using(SqlCommand NrDaten = new SqlCommand(kdquery, updatedb))
{
kdNummer.Value = dataGridView1.Rows[i].Cells["Kundennummer"].Value;
NrDaten.Parameters.Add(kdNummer);
NrDaten.ExecuteNonQuery();
}
using (SqlCommand NameDaten = new SqlCommand(kdquery, updatedb))
{
kdName.Value = dataGridView1.Rows[i].Cells["Kundenname"].Value;
NameDaten.Parameters.Add(kdName);
NameDaten.ExecuteNonQuery();
}
using (SqlCommand MailDaten = new SqlCommand(kdquery, updatedb))
{
kdMail.Value = dataGridView1.Rows[i].Cells["Kundenmail"].Value;
MailDaten.Parameters.Add(kdMail);
MailDaten.ExecuteNonQuery();
}
using (SqlCommand TeleDaten = new SqlCommand(kdquery, updatedb))
{
kdTele.Value = dataGridView1.Rows[i].Cells["Telefon"].Value;
TeleDaten.Parameters.Add(kdTele);
TeleDaten.ExecuteNonQuery();
}
}
updatedb.Close();
}
Your query requires 4 parameters, but you're always only setting one (also please note that in the SqlParameter you need to have the # sign as well). Thus you get an error about missing parameters. Your code should probably look like this:
var kdNummer = new SqlParameter("#Kundennummer", SqlDbType.Int);
var kdName = new SqlParameter("#Kundenname", SqlDbType.VarChar);
var kdMail = new SqlParameter("#Kundenmail", SqlDbType.VarChar);
var kdTele = new SqlParameter("#Telefon", SqlDbType.VarChar);
string kdquery = "INSERT INTO Kunden VALUES (#Kundennummer, #Kundenname, #Kundenmail, #Telefon)";
using (SqlConnection updatedb = new SqlConnection("..."))
{
updatedb.Open();
using (SqlCommand insert = new SqlCommand(kdquery, updatedb))
{
insert.Parameters.Add(kdName);
insert.Parameters.Add(kdNummer);
insert.Parameters.Add(kdMail);
insert.Parameters.Add(kdTele);
for (int i = 0;i<dataGridView1.Rows.Count;i++)
{
kdName.Value = dataGridView1.Rows[i].Cells["Kundenname"].Value;
kdNummer.Value = dataGridView1.Rows[i].Cells["Kundennummer"].Value;
kdMail.Value = dataGridView1.Rows[i].Cells["Kundenmail"].Value;
kdTele.Value = dataGridView1.Rows[i].Cells["Telefon"].Value;
insert.ExecuteNonQuery();
}
}
}
Or even shorter:
string kdquery = "INSERT INTO Kunden VALUES (#Kundennummer, #Kundenname, #Kundenmail, #Telefon)";
using (SqlConnection updatedb = new SqlConnection("..."))
{
updatedb.Open();
for (int i = 0;i<dataGridView1.Rows.Count;i++)
{
using (SqlCommand insert = new SqlCommand(kdquery, updatedb))
{
insert.Parameters.AddWithValue("#Kundenname", dataGridView1.Rows[i].Cells["Kundenname"].Value);
insert.Parameters.AddWithValue("#Kundennummer", dataGridView1.Rows[i].Cells["Kundennummer"].Value);
insert.Parameters.AddWithValue("#Kundenmail", dataGridView1.Rows[i].Cells["Kundenmail"].Value);
insert.Parameters.AddWithValue("#Telefon", dataGridView1.Rows[i].Cells["Telefon"].Value);
insert.ExecuteNonQuery();
}
}
}
EDIT: I modified the long version of the code for maximum re-use of instances. Otherwise you get the errors mentioned in your comment.
I'm familiar with SQL Server but new to Oracle. I'm working on doing a bulk insert or update using the Array technique I've found in some examples. However, I must be doing this incorrect because I'm getting errors about "invalid table.column". I have a feeling it has to do with the way I'm trying to assign parameter names in the in-line SQL and command parameters. In the example I've seen they use numeric values for the parameters like ":1, :2, :3" but if possible I would like to use ":paramtername1, :parametername2, :parametername3".
Here is what I'm doing
sbQuery.Append("update SAP_EMPLOYEE set ");
sbQuery.Append(" EMP_ID = :p_EmployeeId, EVENT_FROM_DT = :p_EventFromDate, EVENT_TYP = :p_EventType, EVENT_RSN = :p_EventRsn,");
sbQuery.Append(" where Emp = :p_Employee");
Then I create arrays to add the values to
string[] arrEmployee = new string[listEmployee.Count];
string[] arrEmployeeId = new string[listEmployee.Count];
DateTime[] arrEventFromDt = new DateTime[listEmployee.Count];
string[] arrEventTyp = new string[listEmployee.Count];
string[] arrEventRsn = new string[listEmployee.Count];
Populate the arrays
int i = 0;
foreach (SAP_EMPLOYEE item in listEmployee)
{
arrEmployee[i] = item.EMP;
arrEmployeeId[i] = item.EMP_ID;
if(item.EVENT_FROM_DT.HasValue)
{
arrEventFromDt[i] = item.EVENT_FROM_DT.Value;
}
arrEventTyp[i] = item.EVENT_TYP;
arrEventRsn[i] = item.EVENT_RSN;
i++;
}
Then call the connection, cmd, query
OracleConnection objConnection = new OracleConnection(connString);
using (objConnection)
{
objConnection.Open();
//create Oracle parameters and pass arrays of data
OracleParameter p_Employee = new OracleParameter();
p_Employee.OracleDbType = OracleDbType.Varchar2;
p_Employee.Value = arrEmployee;
OracleParameter p_EmployeeId = new OracleParameter();
p_EmployeeId.OracleDbType = OracleDbType.Varchar2;
p_EmployeeId.Value = arrEmployeeId;
OracleParameter p_EventFromDate = new OracleParameter();
p_EventFromDate.OracleDbType = OracleDbType.Date;
p_EventFromDate.Value = arrEventFromDt;
OracleParameter p_EventType = new OracleParameter();
p_EventType.OracleDbType = OracleDbType.Char;
p_EventType.Value = arrEventTyp;
OracleParameter p_EventRsn = new OracleParameter();
p_EventType.OracleDbType = OracleDbType.Char;
p_EventType.Value = arrEventRsn;
OracleCommand objCmd = objConnection.CreateCommand();
objCmd.CommandText = sbQuery.ToString();
objCmd.ArrayBindCount = arrEmployee.Length;
objCmd.Parameters.Add(p_Employee);
objCmd.Parameters.Add(p_EmployeeId);
objCmd.Parameters.Add(p_EventFromDate);
objCmd.Parameters.Add(p_EventType);
objCmd.Parameters.Add(p_EventRsn);
objCmd.ExecuteNonQuery();
}
I'm using a stored procedure to update the rows in the column where the consignment number = #consignmentnumber.
Here is a screen of the code.
I think I'm missing something but I can't work out I'm actually missing, can anyone help?
protected void BtnReceived_Click(object sender, EventArgs e)
{
IncrementStatusOfConsignment(sender);
}
private static void IncrementStatusOfConsignment(object sender)
{
var button = (Button) sender;
var gridviewrow = (GridViewRow) button.Parent.Parent;
var consignmentnumber = gridviewrow.Cells[3].Text;
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["Test"].ConnectionString))
{
con.Open();
using (var cmd = new SqlCommand("Test", con))
{
cmd.CommandType = CommandType.StoredProcedure;
var sqlparam = cmd.Parameters.Add("#consignmentnumber", SqlDbType.VarChar);
sqlparam.Value = consignmentnumber;
cmd.ExecuteNonQuery();
}
}
}
Can anyone tell me where I've gone wrong or what I'm missing?
I constantly get this error:
If I set the param to Int it still doesn't work.
If you have declared column consignmentnumber as Numeric(18, 0) then
var sqlparam = cmd.Parameters.Add("#consignmentnumber", SqlDbType.Decimal);
sqlparam.Value = consignmentnumber;
OR something like that:
var sqlparam = cmd.Parameters.Add("#consignmentnumber", SqlDbType.NVarChar, 18);
sqlparam.Value = consignmentnumber;
Am trying to insert several columns into my database using the following insert query from C# but it throws the exception somehow somewhere and my guess is there are no values provided for insertion. i just want to confirm that and find out how i can fix the insert statement. i have a picture below that displays what is passed into the parameters at runtime. i used a break point to get this info.
need your expertise at this one...thanks
if (Page.IsValid)
{
DateTime exhibitDate = DateTime.Now;
int caseid = Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text);
string exhibittype = exhibitTypeTextBox.Text.ToString();
string storedloc = storedLocationTextBox.Text.ToString();
string offid = DropDownList1.SelectedItem.Text.ToString();
Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream;
int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
try
{
SqlConnection connections = new SqlConnection(strConn);
SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID ) VALUES (#CaseID, #ExhibitType, #ExhibitImage, #DateReceived, #StoredLocation, #InvestigationStatus, #OfficerID, #SuspectID, #InvestigatorID, #ManagerID, #AdminID)", connections);
SqlParameter param0 = new SqlParameter("#CaseID", SqlDbType.Int);
param0.Value = caseid;
command.Parameters.Add(param0);
SqlParameter param1 = new SqlParameter("#ExhibitType", SqlDbType.NText);
param1.Value = exhibittype;
command.Parameters.Add(param1);
SqlParameter param2 = new SqlParameter("#ExhibitImage", SqlDbType.Image);
param2.Value = imgBinaryData;
command.Parameters.Add(param2);
SqlParameter param3 = new SqlParameter("#DateReceived", SqlDbType.SmallDateTime);
param3.Value = exhibitDate;
command.Parameters.Add(param3);
SqlParameter param4 = new SqlParameter("#StoredLocation", SqlDbType.NText);
param4.Value = storedloc;
command.Parameters.Add(param4);
SqlParameter param5 = new SqlParameter("#InvestigationStatus", SqlDbType.VarChar, 50);
param5.Value = "";
command.Parameters.Add(param5);
SqlParameter param6 = new SqlParameter("#OfficerID", SqlDbType.NChar, 10);
param6.Value = offid;
command.Parameters.Add(param6);
SqlParameter param7 = new SqlParameter("#SuspectID", SqlDbType.NChar, 10);
param7.Value = null;
command.Parameters.Add(param7);
SqlParameter param8 = new SqlParameter("#InvestigatorID", SqlDbType.NChar, 10);
param8.Value = null;
command.Parameters.Add(param8);
SqlParameter param9 = new SqlParameter("#ManagerID", SqlDbType.NChar, 10);
param9.Value = null;
command.Parameters.Add(param9);
SqlParameter param10 = new SqlParameter("#AdminID", SqlDbType.NChar, 10);
param10.Value = adminID;
command.Parameters.Add(param10);
connections.Open();
int numRowsAffected = command.ExecuteNonQuery();
connections.Close();
if (numRowsAffected != 0)
{
Response.Write("<BR>Rows Inserted successfully");
CaseIDDropDownList.ClearSelection();
exhibitTypeTextBox.Text = null;
storedLocationTextBox.Text = null;
DropDownList1.ClearSelection();
}
else
{
Response.Write("<BR>An error occurred uploading the image");
}
}
catch (Exception ex)
{
string script = "<script>alert('" + ex.Message + "');</script>";
}
the exception is as follows
$exception {"The parameterized query '(#CaseID int,#ExhibitType ntext,#ExhibitImage image,#DateReceive' expects the parameter '#SuspectID', which was not supplied."} System.Exception {System.Data.SqlClient.SqlException}
If you want to pass in NULL for your database / parameter type, you need to use DBNull.Value like this:
SqlParameter param9 = new SqlParameter("#ManagerID", SqlDbType.NChar, 10);
param9.Value = DBNull.Value;
command.Parameters.Add(param9);
Do this wherever you're setting something to null right now, and I'm pretty sure it'll work just fine. Everything is looking okay.
You can do this much easier, try it as such:
if (Page.IsValid)
{
DateTime exhibitDate = DateTime.Now;
int caseid = Convert.ToInt32(CaseIDDropDownList.SelectedItem.Text);
string exhibittype = exhibitTypeTextBox.Text.ToString();
string storedloc = storedLocationTextBox.Text.ToString();
string offid = DropDownList1.SelectedItem.Text.ToString();
Stream imgStream = exhibitImageFileUpload.PostedFile.InputStream;
int imgLen = exhibitImageFileUpload.PostedFile.ContentLength;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
try
{
SqlConnection connections = new SqlConnection(strConn);
SqlCommand command = new SqlCommand("INSERT INTO Exhibits (CaseID, ExhibitType, ExhibitImage, DateReceived, StoredLocation, InvestigationStatus, OfficerID, SuspectID, InvestigatorID, ManagerID, AdminID ) VALUES (#CaseID, #ExhibitType, #ExhibitImage, #DateReceived, #StoredLocation, #InvestigationStatus, #OfficerID, #SuspectID, #InvestigatorID, #ManagerID, #AdminID)", connections);
command.Parameters.AddWithValue("#CaseID", caseid);
//and so on for your 10 parameters
connections.Open();
int numRowsAffected = command.ExecuteNonQuery();
connections.Close();
if (numRowsAffected != 0)
{
Response.Write("<BR>Rows Inserted successfully");
CaseIDDropDownList.ClearSelection();
exhibitTypeTextBox.Text = null;
storedLocationTextBox.Text = null;
DropDownList1.ClearSelection();
}
else
{
Response.Write("<BR>An error occurred uploading the image");
}
}
catch (Exception ex)
{
string script = "<script>alert('" + ex.Message + "');</script>";
}
}
I'm not sure if this will actually fix what is happening without knowing the exact exception. If you could provide the actual exception that would help a lot.