2014-06-17 2 views
1

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로 보내는 이유는 내 질문은 데이터베이스?

+0

컴파일되지 않습니다 게시 된 귀하의 코드 (당신 'ToString()'대신'ToString'을 가지고'TextBox'라는 변수를가집니다. 정확한 문제를 파악할 수 있도록 _exact_ 코드를 게시하십시오. 또는 디버거에서 실행하고 직접 문제를 찾으십시오. –

+0

@DStanley 방금 코드 확인을 마쳤습니다. 나는 몇 가지 수정을해야했다. 내 for 문은 ID에서 알파를 제거하고 알파를 재배치하여 다시 적용해야합니다. 그것은 잘 작동하고 있습니다. 모든 것이 잘 돌아가고 있습니다. 삽입 문이 SQL 측에서 오류를 일으키고 있습니다. – user3549282

+0

디버그가 정상적으로 실행됩니다. – user3549282

답변

0

ID 열은 int입니까? 그렇다면, 아마도 시도 :

cmdins.Parameter.Add("@id", SqlDbType.Int); 
cmdins.Parameter["@id"].Value = Int.Parse(TextBox.Text); 
+0

ID는 알파 numaric입니다 – user3549282

+0

나는 이것을 Char 데이터 형식으로 시도했습니다. 테이블에서 그것은 Char입니다. 나는 여전히 같은 오류를 받았다. – user3549282

0

컴파일되지 않습니다 게시 된 코드는, 그러나 여기 가까이해야 단순화 버전입니다 :

string ID_Sel = "SELECT TOP 1 columnID FROM table ORDER BY columnID"; 
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(); 
var a = cmdsel.ExecuteScalar(); // use ExecuteScalar to get the only value 
int b = Convert.ToInt32(a) + 1; 
textBox.Text = b; 
con.Close(); 
textBox.Attributes.Add("readonly", "readonly"); 
try 
{ 
    con.Open(); 
    cmdins.Parameters.AddWithValue("@id", b); // use the variable instead of the TextBox property. 
    cmdins.ExecuteNonQuery(); 
    con.Close(); 
} 
catch(Exception ex) 
{ 
error.Text = ex.Message; 
} 
+0

변수를 시도했지만 작동하지 않았습니다. – user3549282

+0

"현재 작동 했습니까?"는 의미는 무엇입니까? 같은 오류입니까? –

+0

예. 같은 오류입니다. – user3549282

관련 문제