I searched this title but I didn't find useful thing.
Look at my code:
string q = "exec searchUser {0}";
q = string.Format(q, id);
SqlCommand cm2 = new SqlCommand();
cm2.Connection = sqlCon;
cm2.CommandText = q;
SqlDataAdapter sqldadapter1 = new SqlDataAdapter(cm2);
DataTable dataTabel1 = new DataTable();
sqldadapter1.Fill(dataTabel1);
if (sqlCon.State != ConnectionState.Open)
{
sqlCon.Open();
}
cm2.ExecuteNonQuery();
if (sqlCon.State != ConnectionState.Closed)
{
sqlCon.Close();
}
string uname = dataTabel1.Rows[0][1].ToString();
string logName = dataTabel1.Rows[0][2].ToString();
//DateTime.Now;
int countTrue = listBox_checked.Items.Count;
int countFalse = listBox_LogicFault.Items.Count;
MessageBox.Show(countTrue+"");
MessageBox.Show(countFalse+"");
MessageBox.Show(DateTime.Now.ToString());
DateTime time = DateTime.Now;
//string format = "yyyy-MM-dd HH:mm:ss";
string query_insertReport = "exec insertReport {0},{1},{2},'{3}',{4}, {5},'{6}'";
query_insertReport = string.Format(query_insertReport, id, uname, logName, time, countFalse, countTrue, sendFilename.filename);
SqlCommand cmdInsertReport = new SqlCommand();
cmdInsertReport.Connection = sqlCon;
cmdInsertReport.CommandText = query_insertReport;
if(sqlCon.State != ConnectionState.Open)
{
sqlCon.Open();
}
int idd = Convert.ToInt32 (cmdInsertReport.ExecuteScalar());
MessageBox.Show(idd+"report ID value");
if (sqlCon.State != ConnectionState.Closed)
{
sqlCon.Close();
}
MessageBox.Show("Report Inserted");
I have a problem in the second part of my code where I want to insert report into Reports table and get the each record's reportID with ExecuteScalar()
because I want to insert it in other table. Insert is done successfully but ExecuteScalar()
returned 0 value (wrong value). I have some records in Reports table that numbers start off 32. What's the problem?
Here is my stored procedure:
ALTER PROCEDURE [dbo].[insertReport]
@userID int,
@userName nvarchar(50),
@logicName nvarchar(50),
@date datetime,
@faultNumber int,
@trueNumber int,
@fileName nvarchar(MAX)
AS
INSERT INTO Reports ([UserID], [UserName], [LogicName], [Date],[FaultNumber], [TrueNumber], [filename])
VALUES (@userID, @userName, @logicName, @date, @faultNumber, @trueNumber, @fileName)