ASP.NET 4.0
, C
#, SQL Server 2008 R2
을 사용하고 있습니다.삽입 값이 서버에 수신되지 않습니다.
나는 오류
"Cannot insert the value NULL into the column 'columnID', table 'table name'; column does not allow nulls. INSERT fails. The statement has been terminated."
을받을 내가 마지막 ID 값과 textbox
에, 나는이 테이블에 ID를 삽입 아래의 코드를 볼 수 textbox.text
값을 장소를 얻는 쿼리가 있습니다.
string ID_Sel = "SELECT TOP 1 columnID FROM table ORDER BY columnID DESC";
string ID_Ins = "INSERT INTO table (columnID) VALUES (@id)";
SqlConnection con = new SqlConnection(connection string);
SqlCommand cmdsel = new SqlCommand(ID_Sel, con);
SqlCommand cmdins = new SqlCommand(ID_Ins, con);
con.Open();
SqlDataReader read = cmdsel.ExecuteReader();
read.Read();
string a = read["columnID"].ToString();
for (int i = 0; i < 1; i++)
{
string val1 = a.Substring(1, a.Length - 1);
int b = Convert.ToInt32(val1) + 1;
string val2 = a.Substring(0, a.Length - 6);
a = val2 + b.ToSting("000000");
}
TextBox.Text = a;
con.Close();
TextBox.Attributes.Add("readonly", "readonly");
try
{
con.Open();
cmdins.Parameter.AddWithValue("@id", TextBox.Text);
cmdins.ExecuteNonQuery();
con.Close();
}
catch(Exception ex)
{
error.Text = ex.Message;
}
내 Textbox
표시 레코드에 대한 다음 ID,하지만 내 오류 textbox
표시 :
"Cannot insert the value NULL into the column 'columnID', table 'table name'; column does not allow nulls. INSERT fails. The statement has been terminated."
내 textbox
표시 다음 레코드에 대한 올바른 ID를 수행하지만 null로 보내는 이유는 내 질문은 데이터베이스?
컴파일되지 않습니다 게시 된 귀하의 코드 (당신 'ToString()'대신'ToString'을 가지고'TextBox'라는 변수를가집니다. 정확한 문제를 파악할 수 있도록 _exact_ 코드를 게시하십시오. 또는 디버거에서 실행하고 직접 문제를 찾으십시오. –
@DStanley 방금 코드 확인을 마쳤습니다. 나는 몇 가지 수정을해야했다. 내 for 문은 ID에서 알파를 제거하고 알파를 재배치하여 다시 적용해야합니다. 그것은 잘 작동하고 있습니다. 모든 것이 잘 돌아가고 있습니다. 삽입 문이 SQL 측에서 오류를 일으키고 있습니다. – user3549282
디버그가 정상적으로 실행됩니다. – user3549282